Insert DataTable into nested TreeView c# - c#

I got a database like
CID | CName | CParent
--------+---------+---------
1 | A | 0
2 | b | 1
3 | c | 1
4 | d | 1
5 | e | 2
6 | f | 3
7 | g | 6
8 | h | 2
9 | i | 8
I want to represent this data into a nested treeview connecting each entity to its father which is the CParent.
What is the best way to get this treeview and how?

Related

How to get all ids in LINQ which two columns dont have predefined values

I need to retrieve all Ids from two columns, which doesnt contain some other list.
For example:
If I have a variable:
I need to select List of rows in All notifications by DeliveryTypeId and NotificationGroupId where those IDs not contained in pair combination in selected notifications.
var selectedNotifications = _dbContext.UserNotificationTypeDeliveryChoice.Include(m => m.NotificationGroup)
.Include(m => m.DeliveryType)
.Where(m => m.UserDefId == userDefId && m.UserTypeId == (int)userType)
.Select(m => new NotificationGroup()
{
NotificationGroupId = m.NotificationGroup.NotificationGroupId,
Name = m.NotificationGroup.Name,
DefaultDeliveryType = new DeliveryType()
{
DeliveryTypeId = m.DeliveryType.DeliveryTypeId,
Name = m.DeliveryType.Name,
HasChoosen = true
}
}).ToList();
var selectedNotificationsMatchingIds = selectedNotifications.Select(n => new { n.NotificationGroupId, n.DefaultDeliveryType.DeliveryTypeId }).ToList();
and I need a condition witch will take all rows from db where is !contains.
example:
//NotificationGroup Codelist
+---------------------+-------------+-----------------------+
| NotificationGroupId | Name | DefaultDeliveryTypeId |
+=====================+=============+=======================+
| 1 | Comments | 1 |
+---------------------+-------------+-----------------------+
| 2 | QA | 1 |
+---------------------+-------------+-----------------------+
| 3 | Services | 1 |
+---------------------+-------------+-----------------------+
| 4 | eScheduling | 1 |
+---------------------+-------------+-----------------------+
| 5 | eDelivery | 2 |
+---------------------+-------------+-----------------------+
//Delivery Type Codelist
+----------------+-----------------+
| DeliveryTypeId | Name |
+================+=================+
| 1 | SMS |
+----------------+-----------------+
| 2 | Email |
+----------------+-----------------+
| 3 | PortalMessaging |
+----------------+-----------------+
//SELECTED NOTIFICATIONS
+--------------------------------------+-----------+----------------------------+---------------------+----------------+------------+
| UserNotificationTypeDeliveryChoiceId | UserDefID | UserCompanyOrInstitutionId | NotificationGroupId | DeliveryTypeId | UserTypeId |
+======================================+===========+============================+=====================+================+============+
| 269 | 2933 | NULL | 1 | 2 | 1 |
+--------------------------------------+-----------+----------------------------+---------------------+----------------+------------+
| 270 | 2933 | NULL | 2 | 2 | 1 |
+--------------------------------------+-----------+----------------------------+---------------------+----------------+------------+
| 271 | 2933 | NULL | 3 | 2 | 1 |
+--------------------------------------+-----------+----------------------------+---------------------+----------------+------------+
| 272 | 2933 | NULL | 4 | 2 | 1 |
+--------------------------------------+-----------+----------------------------+---------------------+----------------+------------+
| 273 | 2933 | NULL | 4 | 1 | 1 |
+--------------------------------------+-----------+----------------------------+---------------------+----------------+------------+
//ALL NOTIIFICATIONS
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| NotificationGroupUserTypeId | NotificationGroupChangeTypeId | UserTypeId | NotificationGroupId | NotificationTemplateId | DateCreated | DeliveryTypeId |
+=============================+===============================+============+=====================+========================+=========================+================+
| 1 | NULL | 1 | 1 | 14 | NULL | 1 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 6 | 11 | 1 | 4 | 21 | 2011-07-18 11:13:23.543 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 17 | NULL | 1 | 4 | 6 | NULL | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 26 | NULL | 1 | 3 | 8 | NULL | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 30 | 11 | 1 | 4 | 15 | 2011-07-17 13:16:36.897 | 1 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 31 | 14 | 1 | 4 | 16 | 2011-07-17 13:17:00.880 | 1 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 32 | 15 | 1 | 4 | 17 | 2011-07-17 13:17:18.220 | 1 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 33 | 16 | 1 | 4 | 18 | 2011-07-17 13:17:34.833 | 1 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 34 | 17 | 1 | 4 | 19 | 2011-07-17 13:17:58.337 | 1 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 35 | 18 | 1 | 4 | 20 | 2011-07-17 13:18:35.960 | 1 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 36 | 14 | 1 | 4 | 22 | 2011-07-18 16:00:27.320 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 37 | 15 | 1 | 4 | 23 | 2011-07-18 16:01:17.843 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 38 | 16 | 1 | 4 | 24 | 2011-07-18 16:01:36.300 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 39 | 17 | 1 | 4 | 25 | 2011-07-18 16:01:55.923 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 40 | 18 | 1 | 4 | 26 | 2011-07-18 16:02:12.607 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 41 | 8 | 1 | 3 | 27 | 2011-11-09 12:59:51.307 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 44 | 19 | 1 | 3 | 28 | 2011-12-21 14:57:31.780 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 48 | 20 | 1 | 5 | 29 | 2011-12-23 10:19:16.840 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
| 49 | 1 | 1 | 3 | 6 | 2011-12-22 16:09:15.047 | 2 |
+-----------------------------+-------------------------------+------------+---------------------+------------------------+-------------------------+----------------+
So I am getting in selectedNotifications correct pairs, but then when I try to filter my new query, I m getting only two pairs but I should get it (with these example):
var notSelectedNotifications = _dbContext.NotificationGroupUserType.Include(m => m.DeliveryType)
.Include(m => m.NotificationGroup)
.Where(m => m.UserTypeId == (int)userType)
.Where(m => !selectedNotificationsMatchingIds.Contains(new { m.NotificationGroup.NotificationGroupId, m.DeliveryType.DeliveryTypeId }))
.Select(m => new NotificationGroup()
{
NotificationGroupId = m.NotificationGroupId,
Name = m.NotificationGroup.Name,
DefaultDeliveryType = new DeliveryType()
{
DeliveryTypeId = m.DeliveryType.DeliveryTypeId,
Name = m.DeliveryType.Name,
HasChoosen = false
}
}).ToList();
Any advise is welcomed. If someone can tell me, how to get all of these which is not selected in selectedNotificationsMatchingIds
The Contains() method usage default equality comparer when implementation of IEqualityComparer<T> is not provided for element stored in collection. Obviously the default equality comparer will not work for element of Anonymous class stored in selectedNotificationsMatchingIds.
You have two options:
Option 1: Re-write Where condition to use Any as:
//.Where(m => !selectedNotificationsMatchingIds
// .Contains(new { m.NotificationGroup.NotificationGroupId,
// m.DeliveryType.DeliveryTypeId }))
.Where(m => !selectedNotificationsMatchingIds
.Any(c => c.NotificationGroup.NotificationGroupId == m.NotificationGroupId
&& c.DeliveryTypeId == m.DeliveryType.DeliveryTypeId))
Option 2: Create a class having properties for NotificationGroupId and DeliveryTypeId and implement IEqualityComparer<T> interface for it. Then only you can use object of that class as part of .Contains() method.

how to Group By LINQ Query result to DataGridView without looping

I Have a list "listemp" :
id | name | age | departement
----|--------|------|----------
1 | name1 | 22 | 1
2 | name2 | 33 | 3
3 | name3 | 20 | 1
4 | name4 | 25 | 2
5 | name5 | 22 | 1
6 | name6 | 25 | 2
I want to group by departement
the result must be like that :
id | name | age | departement
----|--------|------|----------
1 | name1 | 22 | 1
3 | name3 | 20 | 1
5 | name5 | 22 | 1
4 | name4 | 25 | 2
6 | name6 | 25 | 2
2 | name2 | 33 | 3
My code is :
var db = from item in listemp group item by item.departement;
dgv.columns.add("0","id");
dgv.columns.add("1","name");
dgv.columns.add("2","age");
dgv.columns.add("3","departement");
foreach(var i in db)
{
foreach(var c in i)
{
dgv.rows.add(c.id , c.name , c.age , c.departement)
}
}
this code is work perfectly but I want another methode to do that without looping
I find the solution without looping
var db = from item in listitem group item by item.departement into g from i in g select i

How to query the sum of each person's availability base on their skill

I'm using sqlite, I've setup three tables.
Table for list of skills this can be expand.
+----+----------+
| ID | Skills |
+----+----------+
| 1 | Swimming |
| 2 | Running |
| 3 | Boxing |
| 4 | Dancing |
| 5 | Singing |
+----+----------+
Table for availability of each person. 0 represents the person is not available.
+-------+-----+-----+-----+-----+-----+-----+-----+
| Names | SUN | MON | TUE | WED | THU | FRI | SAT |
+-------+-----+-----+-----+-----+-----+-----+-----+
| Mark | 0 | 1 | 1 | 1 | 1 | 1 | 0 | (MON-FRI)
| Robin | 0 | 1 | 1 | 1 | 1 | 1 | 0 | (MON-FRI)
| James | 0 | 0 | 1 | 1 | 1 | 1 | 1 | (TUE-SAT)
+-------+-----+-----+-----+-----+-----+-----+-----+
Table for each person skill possess. 1/0 is a boolean value.
+-------+----------+---------+--------+---------+---------+
| Names | Swimming | Running | Boxing | Dancing | Singing |
+-------+----------+---------+--------+---------+---------+
| Mark | 1 | 1 | 1 | 1 | 1 |
| Robin | 1 | 1 | 1 | 1 | 1 |
| James | 1 | 1 | 1 | 1 | 0 |
+-------+----------+---------+--------+---------+---------+
By using the tables above how could I achieved this result to sum each person's availability base on their skill.
Expected Results:
+----------+-----+-----+-----+-----+-----+-----+-----+
| Skills | SUN | MON | TUE | WED | THU | FRI | SAT |
+----------+-----+-----+-----+-----+-----+-----+-----+
| Swimming | 0 | 2 | 3 | 3 | 3 | 3 | 1 |
| Running | 0 | 2 | 3 | 3 | 3 | 3 | 1 |
| Boxing | 0 | 2 | 3 | 3 | 3 | 3 | 1 |
| Dancing | 0 | 2 | 3 | 3 | 3 | 3 | 1 |
| Singing | 0 | 2 | 3 | 3 | 3 | 3 | 0 |
+----------+-----+-----+-----+-----+-----+-----+-----+

Show data from two model with same column name same value in ASP.NET

I'm new to ASP.NET, I have only experiences in C# Windows Form and SQL Server. Now I have starting my new project in ASP.NET MVC and already have database from my customer. It has two model included some same column name like this:
Table 1
---------------------------------
| Location | Item | Model | Tag |
---------------------------------
| 1 | 10 | A5 | 221 |
| 2 | 10 | A6 | 233 |
| 3 | 12 | A8 | 332 |
| 4 | 15 | C1 | 223 |
Table 2
-------------------------------------------------
| Location | Item | Model | Tag | DWeek | DYear |
-------------------------------------------------
| 1 | 10 | A5 | 221 | 01 | 15 |
| 2 | 10 | A6 | 233 | 01 | 15 |
| 3 | 12 | A8 | 332 | 02 | 15 |
| 4 | 15 | C1 | 223 | 03 | 15 |
I just want to show the data which have same Location and Item in one table, I don't know how to query in Entity framework, I have only know how to code by SQL, and I think it seem to be like this:
select
r.Location
,r.Item
,d.Location
,d.Item
,d.DWeek
,d.DYear
,r.Model
,r.[Tag No]
,d.[Tag No]
from Register r, Due d
where r.Location = d.Location
and r.Item = d.Item
My question is how to query like this in Entity framework for show data in a view, and is it possible to update and create two model in the same time?
I am using Lambda Expression
var Query=Register.Join(Due,r=>r.Location,d=>d.Location,(r,d)=>new {r,d}).Where(
X=>X.r.Location = X.d.Location && X.r.Item = X.d.Item).Select(X=>new {
LOCATION_1 =X.r.Location;
ITEM_1=X.r.Item;
LOCATION_2=X.d.Location;
ITEM_2=X.d.Item;
DWEEK=X.d.DWeek;
DTEAR=X.d.DYear;
MODEL=X.r.Model;
TAGNO_1=X.r.Tag_No;
TAGNO_1=X.d.Tag_No;
}).ToList();

MySQL Query to retrieve data from two tables even no data found and unique column in both tables

I have two tables.
Menu
+----------------+----------------+
| menu_id | menu_desc |
+----------------+----------------+
| 1 | menu1 |
| 2 | menu2 |
| 3 | menu3 |
| 4 | menu4 |
| 5 | menu5 |
+----------------+----------------+
Rights
+----------+--------------+---------+
| Role_id | menu_id | Rights |
+----------+--------------+---------+
| 1 | 1 | 3 |
| 1 | 2 | 3 |
| 1 | 3 | 3 |
+----------+--------------+---------+
I want the output something like this,
+----------+------------------+------------+-----------+
| menu_id | menu_desc | Role_id | Rights |
+----------+------------------+------------+-----------+
| 1 | menu1 | 1 | 3 |
| 2 | menu2 | 1 | 3 |
| 3 | menu3 | 1 | 3 |
| 4 | menu4 | 1 | null |
| 5 | menu5 | 1 | null |
+----------+------------------+------------+-----------+
Is it possible?
This gives NULL for both Role_id and Rights
SELECT Menu.menu_id, Menu.menu_desc, Role.Role_id, Role.Rights
FROM Menu LEFT OUTER JOIN Role ON Menu.menu_id=Role.menu_id
ORDER BY Menu.menu_id
try below, if not getting required results then share problem:
SELECT m.menu_id, m.menu_desc, r.Role_id, r. Rights FROM Menu m LEFT JOIN `Rights` r ON m.menu_id=r.menu_id order by m.menu_id;
You have to use join:
select Menu.menu_id, menu_desc, Role_id, Rights.menu_id, Rights.Rights from Menu join Rights on Menu.menu_id=Rights.menu_id;

Categories

Resources