user1828605 user1828605 - 1 year ago 161
SQL Question

Convert SQL to lambda in C#

How can I re-write the following query from SQL to lambda in C#? Is it recommended to write in lambda?

FROM Claims
FROM MissingItems
GROUP BY claimId);

Answer Source

Equivalent using LINQ lambdas - assuming you have a collection of MissingItem objects in your code (and a representation of Claim in your code)

List<String> distinctClaimIds = missingItems.Select(mi => mi.claimId).Distinct();

List<Claim> claimsWithThoseIds = claims.Where(c => distinctClaimIds.Contains(;

Edit for your "one statement" interest:

Closest to "one statement" (even though I think 2 is more readable) I can think of:

List<Claim> claimsWithThoseIds = claims.Where(c => missingItems.Select(mi => mi.claimId).Distinct().Contains(
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download