运行下面的查询并在调试时将鼠标悬停在"usersToWork“上之后,我可以查看返回给我的单个条目的所有属性,以及与该值有关系的其他表。我需要向用户显示的是"Lines.Id“值(Lines是表,Id是Lines表中的列),但是这个值在SelectMany()语句中丢失了。有没有办法选择"Lines.Id“值来包含在我从所有连接中获得的最终值中?在下面的代码中,我注释掉了我想要的东西,但是我不能把它放在那里,否则我会在第一个SelectMany语句中得到错误,说'int‘不包含’Shift‘的定义,并且没有扩展方法’Shift‘接受类型为'int’的第一个参数。
如果我错了,请纠正我,但是SelectMany()选择了您想要连接的所有列。在本例中,在第一个SelectMany()中,我仅从“Shift”表中获取值,在第二个SelectMany()中,我仅从"Users“表中获取值。为什么这与SQL连接不同?在连接SQL时,您可以获得每一列,因为您将它们连接在一起,SelectMany()只产生您连接的第二个表的值。是否有可能在"Lines“表中获取该值,或者我是否需要执行另一次查询?任何帮助都是最好的。
int idEnteredByUser = 123;
var usersToWork = entityDataModel.Lines
//....NOT IN MY CODE NOW....
// .Select(line => line.Id)//THIS IS WHAT I NEED.
// .Select(line => line.Description, line.Id//OR THIS TO RETURN TWO VALUES IF POSSIBLE
//This is my current code, I need to include on of the select lines above.
.SelectMany(line => line.Shifts) //Join lines on shifts.
.Where(shift => shift.EndTime >= DateTime.Now) //Join restricted times.
.SelectMany(user => user.Users) //Join the restricted shift times on users.
.Where(user => user.UserId == idEnteredByUser ); //Only look for the specific user
发布于 2018-02-28 16:29:21
使用LINQ查询语法可以更轻松地实现这一点。
我假设您在发布的代码中犯了一个拼写错误,并且user
是shift
的一个属性。
var idEnteredByUser = 123;
var usersToWork =
from line in entityDataModel.Lines
from shift in line.Shifts
where shift.EndTime >= DateTime.Now
from user in shift.Users
where user.UserId == idEnteredByUser
select new
{
Description = line.Description,
Id = line.Id
};
https://stackoverflow.com/questions/49033812
复制