Over a million developers have joined DZone.

Determining the Difference Between Two Entity Sets Using LINQ

·
In this post I am going to discuss how to determine the difference between two table/ entity sets. Here's an example: we want to get rows form table_A which do not exist in table_B. (When table_A primarykey is part of table_B foreignkey).

What follows is a way to get the row(s) to form table_A which are not part of table_B

SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
WHERE B.Key IS NULL
</select_list>

In Linq to Sql you can achieve same thing just by calling one function exception.

var infoQuery =
(from tblA in db.Table_A
select tblA.Key)
.Except
(from tblB in db.Table_B
select tblB.Key);

So by using above query you can easily achieve task of creating a difference between two entity sets.
Topics:

Published at DZone with permission of Pranay Rana, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}