Home > Net >  convert from sql to linq
convert from sql to linq

Time:12-24

columns id,CompanyId,CustomerId table data

select * from SuddathCustomerToCompnayMapping where CompanyId in( select CompanyId from SuddathCustomerToCompnayMapping where customerid=4455)

i want to use linq with subquery on same table

i tried to use .equals but it does not work then i am using subquery

CodePudding user response:

var results = from mapping in SuddathCustomerToCompnayMapping
              where (from m in SuddathCustomerToCompnayMapping
                     where m.CustomerId == 4455
                     select m.CompanyId).Contains(mapping.CompanyId)
              select new { mapping.Id, mapping.CompanyId, mapping.CustomerId };

You could also replace .Contains(...) with .Any(id => id == mapping.CompanyId)

CodePudding user response:

with this example you don't need subquery.

you just filter table with CustomerId.

TSQL:

select * from SuddathCustomerToCompnayMapping where CustomerId = 4455

Linq:

from suddathCustomerToCompnayMapping in SuddathCustomerToCompnayMapping
where suddathCustomerToCompnayMapping.CustomerId == 4455
select suddathCustomerToCompnayMapping 
  • Related