question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked TimonYang-MSFT edited

How to mention multiple columns in join when using LINQ Method syntax for join

This is sample LINQ method syntax join where single column used in join condition.

DataTable _dtMaster = dt();
_dtMaster.Columns.Add("Discription");
DataTable _dtChild = dttt();
_dtMaster.AsEnumerable().Join(_dtChild.AsEnumerable(), _dtmater => Convert.ToString(_dtmater["id"]),
_dtchild => Convert.ToString(_dtchild["id"]), (_dtmater, _dtchild) => new { _dtmater, _dtchild }).ToList().ForEach(o=>o._dtmater.SetField("Discription",o._dtchild["Discription"].ToString()));

Please tell me how to mention multiple columns in join when i will follow above approach method syntax?

please give me a sample code using above approach kind of LINQ where multiple columns will be used in join.

Thanks

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
1 Vote"
cooldadtx answered TimonYang-MSFT edited

You just use an anonymous type.

from m in _dtMaster
join c in _dtChild on new { Id = m["id"], SomeValue = m["category"] } equals new { Id = c["masterId"], SomeValue = c["subcategory"] }
select new { ... }
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sorry sir i want syntax in terms of LINQ method. the code i have provided is LINQ method type. so i like to know how to mention multiple columns in join when use LINQ method syntax. please share LINQ join code using LINQ method approach. thanks

0 Votes 0 ·

Try this:

  _dtMaster.AsEnumerable().Join(_dtChild.AsEnumerable(), _dtmater => new { id = _dtmater["id"],type=_dtmater["Type"] },
             _dtchild =>new { id = _dtchild["id"], type = _dtchild["Type"] }, (_dtmater, _dtchild) =>
             new { _dtmater, _dtchild }).ToList().ForEach(o => o._dtmater.SetField("Discription", o._dtchild["Discription"].ToString()));

This is actually no different from the above answer. Query syntax will be compiled into method syntax and then executed.
Query Syntax and Method Syntax in LINQ (C#)

1 Vote 1 ·

Sir Thank you so much.

0 Votes 0 ·