Datatable - Using linq I want to update a row with the count - c#

I have a C# datatable with:
Id ParentId Name
======== =========== =====
1 A
2 1 b
3 1 c
4 2 d
6 5 e
I want to add a column or just the result that will identify any ParentIds that do not have an Id. Basically in the above table, I want to find Id=6 since ParentId 5 is invalid.
I want to identify anywhere a parentId is not listed as an Id
I thought this could be accomplish with Linq.

This will return all rows where the ParentId does not appear in any row as Id
rows.Where(r1 => !rows.Any(r2 => r1["ParentId"] == r2["Id"]))

Related

How to Update Column based on another table column value

I have two data tables
table
id isfav
---------
1 1
2 1
3 1
4 1
5 1
favtable id
-----------
2 true
3 false
So I want to update the table1 column isFav to 0 if the ids exist in FavTable with false.
Can anybody help me with this?
You can use Any() to search in other entities.
var db = new YourDbContext();
var favtable = db.favtable.ToList();
//Find them:
var result = db.Table1.Where(t => favtable.Any(f => f.id == t.id && !f.isfav));
//result should be 3.
.NET Fiddle: https://dotnetfiddle.net/BmaqN5

Linq where on include

I'm stuck with this linq query.
I've this tables.
ID A B C D
1 some data
2 some other data
Then, For every record on that table I may have none or many rows
ID TableA_ID R
1 1 1
2 1 2
3 1 5
4 2 2
For example. Row 1 (some data) has 3 rows on table B.
I tried using
tableA.Include(x => x.tablebchilds.Where( d => d.R == 1)).ToList()
but it is not working. With many others varation.
The objective of this query is to return tableA.row #1 if I pass it 1 as value (value of R). Number <> 2 won't give any result.
Tables are linked on EF. So TableB.tableA_ID is Foreign key of tableA.ID
Edit #1
I tried the answers in the question marked as duplicated with no luck. Give that 2 tableA.rows if the user insert 1 as parameter, linq query should return Row #1, some data. If 2 is passed as parameter, nothing is return.
A working SQL statement is:
SELECT [TableA].* FROM [TableA] JOIN [TableB] ON [TableA].[Id] = [TableB].[TableA_Id] WHERE [TableB].[R] = 1
Thanks!
If you have database relationship properly configured this have to work.
tableA.Include(x => x.tableBChilds).Where(tableA => tableA.tableBChilds.Any(b => b.R== 1)).ToList();

Mapping values in dropdown

Table 1:
ID
1
2
3
4
5
6
7
8
9
0
I have to create dropdowns called MasterID & ChildID out of this ID column. MasterID & ChildID will have ID column values based on mapping. Mapping process will be like any values from the ID column can be MasterID or ChildID. But MasterID will have unique ChildID.
Note: While binding MasterID and ChildID dropdowns it will have ID values on pageload. Once mapping starts then the values should come accordingly. Also I have a Add button in my page and once I select values in two dropdowns and add then they will get saved in the DB.
For ex: If I select the ID value 1 in MasterID and 5 in ChildID and add them to DB, then the next time the value 1 from ID column can come in MasterID dropdown but not in ChildID. Also the value 5 from ID column is mapped to ChildID and saved, where this value should not appear in MasterID and ChildID dropdowns. Once any value is selected as the ChildID and saved, then it shouldn't get displayed inMasterIDor inChildID` dropdowns.
Final Output should be like this.
Table 2:
MasterID ChildID
1 5
2 3
2 6
4 7
4 8
9 0
Can anyone help me or suggest how I can achieve this for asp.net web application.
You can use LINQ to filter the values that you need to display in both the dropdowns.
Something like following.
int[] table1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int[] tableMaster = new int[] { 1, 2, 2, 4, 4, 9 };
int[] tableChild = (from t in table1
where !tableMaster.Contains(t)
select t).ToArray();
Note: This is just an example so that you get an idea.

Issues With LINQ to SQL Query

I am having trouble getting one of my LINQ to SQL queries to work correctly. I have three tables setup as follows:
Vendors
id - primary key
Manufacturers
id - primary key
ManufacturerVendorRelationships
id - primary key
manufacturer_id - foreign key to manufacturer.id
vendor_id - foreign key to vendor.id
I am trying to write a LINQ to SQL query that will fetch the Vendors that are not currently related to a given manufacturer. For example, the following table data should only result in 2 vendors for the manufacturer with ID of 1. Can someone please help me with the LINQ to SQL for this example? Right now, the application is doing this logic. Thanks in advance.
Vendors
ID
1
2
3
4
5
Manufacturers
ID
1
2
3
4
5
ManufacturerVendorRelationships
ID ManufacturerID VendorID
1 1 1
2 1 2
3 1 3
Maybe something like this:
var result=(
from v in db.Vendors
where !db.ManufacturerVendorRelationships
.Select(s=>s.VendorID)
.Contains(v.ID)
select v
);
Or if you want it like a field:
var result=(
from v in db.Vendors
select new
{
v.ID,
HasManufacturer=db.ManufacturerVendorRelationships
.Select(s=>s.VendorID)
.Contains(v.ID)
}
);
where db is the linq data context
int id= 1;
var result =
dc.Vendors
.Where(v => !dc.ManufacturerVendorRelationships
.Any(rel => rel.VendorId == v.Id && rel.ManufacturerId == id));

Get the first record of a group in LINQ? [duplicate]

This question already has answers here:
How to get first record in each group using Linq
(7 answers)
Closed 5 years ago.
Summary: How to get top 1 element in ordered groups of data
I am trying to group by a CarId field , and then within each group, I want to sort on a DateTimeStamp field descending. The desired data would be for each Car give me the latest DateTimeStamp and only that 1 in the group.
I can get to this point, but having problems taking the top 1 off of the group and ordering the group by DateTimeStamp desc.
Here is what I have after the first group operation:
group 1
------------------------
CarId DateTimeStamp
1 1/1/2010
1 1/3/2010
1 3/4/2010
group 2
------------------------
CarId DateTimeStamp
2 10/1/2009
2 1/3/2010
2 9/4/2010
what I would desire only the top 1 in an ordered group
group 1
------------------------
CarId DateTimeStamp
1 3/4/2010
group 2
------------------------
CarId DateTimeStamp
2 9/4/2010
Brickwall: Where I get stopped, is needing the CarId and DateTimeStamp in the group by clause, in order to later sort by the DateTimeStamp. Maybe the sorting of the date should be done in a separate function, not sure.
data
.GroupBy(
x => x.CardId,
(x, y) => new {
Key = x,
Value = y.OrderByDescending(z => z.DateTimeStamp).FirstOrDefault()
}
);
This will group all the elements by CardId, then order the elements of each group by DateTimeStamp (descending), then pare down each group to only contain the first element. Finally, it returns an enumerable of "groups" (with the scare quotes since they're actually an anonymous type instead of an IGrouping) where each group has the one item you're seeking.
I find the query expression syntax easier to understand myself:
from x in data
group x by x.CarId into grp
select new {
CarId = x.CarId,
Record = (from x in grp orderby z.DateTimeStamp descending).FirstOrDefault()
};

Categories