Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ with joins and a List<GUID>

Here's the SQL that I used to prototype the LINQ statement. The GUID's in code are List<GUID>. I tried for 3 hours and just don't get it. Could some kind sole who can write LINQ in their sleep help me out.

SELECT DISTINCT  [id]
      ,[emailAddress]
      ,[name]
      ,[emailRunNumber]
  FROM [emailAddress]
  join promotionsJoin
  on promotionsJoin.EmailAddressId = emailAddress.id
  where promotionsJoin.promotionId in ('09464b57-f3d7-41ec-b0b1-cbc5999824bd',
  '8bc855b2-2f01-4083-b43a-dab7b7a81ac8') AND emailRunNumber is NULL
like image 889
George Aguiar Avatar asked Nov 14 '22 05:11

George Aguiar


1 Answers

(from emailAddress in emailAddresses
 join promotion in promotions 
   on emailAddress.id equals promotion.EmailAddressId
 where guidsList.Contains(promotion.promotionId)
    && promotion.emailRunNumber == null
 select new {
     emailAddress.id,
     emailAddress.emailAddress,
     promotion.name,
     promotion.emailRunNumber
}).Distinct()

The join here will be unnecessary if there's already a relationship between the two tables in your mapping (then promotion will just be a field off of emailAddress)

like image 176
bdukes Avatar answered Dec 20 '22 07:12

bdukes