How to join two LINQ queries

c# entity-framework-6 linq

Accepted Answer

You can easily use both sets of data usingSelectMany . Query syntax for this would be:

var eventsQry =
    from eventDocument in eventDocuments
    where eventDocument.LastName.StartsWith(FilterLastName)
    from ev in events
    where ev.EventData.StartsWith(FilterCriteria) && (ev.ID1 == eventDocument.ID1) && (ev.ID2 == eventDocument.ID2)
    select new { eventDocument, ev };

You don't have to filter your results using just one query. Multiple requests may be merged:

var eventsQry =
    from ev in events
    where ev.EventData.StartsWith(FilterCriteria)
    select ev

var documentsQry =
    from eventDocument in documentsQry
    where eventDocument.LastName.StartsWith(FilterLastName)
    select eventDocument;

var combinedQry =
    from eventDocument in documentsQry
    from ev in eventsQry
    where (ev.ID1 == eventDocument.ID1) && (ev.ID2 == eventDocument.ID2)
    select new { eventDocument, ev };
1
5/3/2017 7:59:26 AM


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