我在编写LINQ查询时遇到问题。 这是场景:
我有2个数据库:A和B. 在数据库A中:我有一个tableX,其中包含以下字段:员工ID,姓名,地址,电话,......,活动
在数据库B中:我有一个tableY,其中包含以下字段:Employee ID,Visible,Order
表Y中的记录数小于或等于表X中的记录数。
基本上,我需要从表X中提取员工记录,这些记录的属性“Visible”(在表Y中)设置为True,并希望使用“Order”属性对它们进行排序。
这是我到目前为止所做的:
ADataContext dbA = new ADataContext();
BDataContext dbB = new BDataContext();
//Get the list of records from tableY where 'Visbile' is set to True
var List = dbB.tableY
.Where(x => x.Visible == true).OrderBy(x => x.Order)
.ToList();
//Extract the list of employee IDs
IEnumerable<int> ids = List.Select(x => x.EmployeeID).Distinct();
var employees = dbA.tableX
.Where(x => ids.Contains(x.EmployeeID) && x.Active == true)
.ToList();
我能够获得正确的员工列表,但无法弄清楚如何在tableX上应用排序顺序(在tableY中) 目前,无论tableY中指定的顺序如何,从tableX返回的记录都按照在表中输入的顺序排序(从最旧到最近)。
任何想法如何解决我的问题。
谢谢,
答案 0 :(得分:0)
我把它全部重写为一个查询:
var employees =
from x in dbA.tableX
where x.Active
from y in dbB.tableY
where x.EmployeeID == y.EmployeeID
orderby y.Order
select x;