Consider this LINQ expression written using query notation:
List<Person> pr = (from p in db.Persons join e in db.PersonExceptions on p.ID equals e.PersonID where e.CreatedOn >= fromDate orderby e.CreatedOn descending select p) .ToList();
Question: how would you write this LINQ expression using dot notation?
When you use the LINQ join clause in the query expression syntax to combine two sets of related information, you perform an inner join.
In LINQ, an inner join is used to serve a result which contains only those elements from the first data source that appears only one time in the second data source. And if an element of the first data source does not have matching elements, then it will not appear in the result data set.
In a LINQ query expression, join operations are performed on object collections. Object collections cannot be "joined" in exactly the same way as two relational tables. In LINQ, explicit join clauses are only required when two source sequences are not tied by any relationship.
You probably found out that you can't join an Entity Framework LINQ query with a local list of entity objects, because it can't be translated into SQL. I would preselect the database data on the account numbers only and then join in memory.
Like this:
List<Person> pr = db.Persons .Join(db.PersonExceptions, p => p.ID, e => e.PersonID, (p, e) => new { p, e }) .Where(z => z.e.CreatedOn >= fromDate) .OrderByDescending(z => z.e.CreatedOn) .Select(z => z.p) .ToList();
Note how a new anonymous type is introduced to carry both the p
and e
bits forward. In the specification, query operators which do this use transparent identifiers to indicate the behaviour.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With