开发者

Subtraction in LINQ

开发者 https://www.devze.com 2023-03-07 17:34 出处:网络
I have two tables: \"Personnel\" with ID,Name,... columns and \"User\" with PersonnelID (FK of ID column in \"Personnel\"),Username,Password columns

I have two tables: "Personnel" with ID,Name,... columns and "User" with PersonnelID (FK of ID column in "Personnel"),Username,Password columns

The question is how can I query the personnel entities which their IDs are not used in "User" table, I mean entities from "Personnel" with no record in "User" table?

At 开发者_StackOverflow社区first I thought creating a view containing each "Personnel" entity with record in "User" table and subtracting this view from all Personnel entities, is the solution, but I don't know how to subtract two views in Linq

I used this query for the view:

from p in ObjectContext.personnels
join u in ObjectContext.users on p.ID equals u.PersonnelID 
select p;

Is this the solution? If yes how can I subtract two views?

Or is there a better way?

PS!!: Sorry for my bad english :D


The question is how can I query the personnel entities which their IDs are not used in "User" table

Answer:

from p in ObjectContext.personnels
where !ObjectContext.users.Any(u => u.PersonnelID == p.ID)
select p;

Although, you might want to limit the columns that you pull:

from p in ObjectContext.personnels
where !ObjectContext.users.Any(u => u.PersonnelID == p.ID)
select new { p.Id, p.Name, etc };
0

精彩评论

暂无评论...
验证码 换一张
取 消