EF Core Linq Join with OR condition using Method Systax

.net c# entity-framework-core linq

Question

How can I make this into a single query?

var result1 = await _dbContext.Emails.Join(_dbContext.Sms,
                                    pn => pn.PNID,
                                    sms => sms.From_PNID.Value,
                                    (pn, sms) => new { sms, pn });


var resut2 = await _dbContext.Emails.Join(_dbContext.Sms,
                                    pn => pn.PNID,
                                    sms => sms.To_PNID.Value,
                                    (pn, sms) => new { sms, pn });

Want to have like ON pn.PNID == sms.To_PNID OR pn.PNID == sms.From_PNID

1
4
7/3/2018 11:14:43 PM

Accepted Answer

You can try to do it using SelectMany extension method:

var result1 = await  dbContext.Emails
                    .SelectMany(e=>dbContext.Sms.Where(s=>e.PNID==From_PNID.Value 
                                                       || e.PNID==To_PNID.Value)
                                                .Select(e=>new {Email=e,SMS=s})).ToListAsync();
0
7/3/2018 2:59:24 PM

Popular Answer

You can use conventional LINQ instead of LINQ expression.

e.g.

     var records = (from recordA in TableA 
                    from recordB in TableB 
                    where recordA.Column_1 == recordB.Column_1 || recordA.Column_2 == recordB.Column_2
                   ).ToList();


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow