Surya sasidhar
Surya sasidhar

Reputation: 30313

Linq join with more than one condition?

In linq i am trying to do like this

select * from tbl1 join tbl2 on tbl1.column1= tbl2.column1 and tbl1.column2 = tbl2.column2

how can i write the above query in Linq.... i tried like this but giving error

  var sasi = from table1 in dtFetch.AsEnumerable()
             join table2 in dssap.AsEnumerable() 
             on new { 
                table1.Field<string >["SAPQuotationNo"],
                table1.Field<string >["Invoiceno"]} 
             equals new {  
                table2.Field<string>["SAPQuotationNo"],
                table2.Field <string>["Invoiceno"]
             }

Upvotes: 1

Views: 120

Answers (2)

Iti Tyagi
Iti Tyagi

Reputation: 3661

You can try something like this:

from A in context.A
join B in context.B on new { id = B.ID,//..., type = A.ID,//...} 

This is the hint, you can explore.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460128

  • Use anonymous types
  • give the properties names
  • select something
  • use DataRow.Field as method with round brackets

var sasi = from table1 in dtFetch.AsEnumerable()
           join table2 in dssap.AsEnumerable() 
           on new 
           { 
               SAPQuotationNo = table1.Field<string>("SAPQuotationN"),
               Invoiceno = table1.Field<string>("Invoiceno")
           } equals new 
           {
               SAPQuotationNo = table2.Field<string>("SAPQuotationNo"),
               Invoiceno = table2.Field<string>("Invoiceno") 
           }
           select table1;

Upvotes: 5

Related Questions