Linq to Entities:无法使集合查询工作
我正在努力使用 L2E 获取记录集合。这是模型视图: http://pascalc.nougen.com/stuffs/aspnet_linq_model2.png
我有一个用户标识符,它与 1 个或多个用户组关联,这些用户组本身链接到测试用例。我想获取与用户 ID X 关联的所有组的所有测试用例。
我还注意到,我没有获得与 2 个(或更多)关联的用户的所有项目。
到目前为止,我是这样做的:
QASModel.QASEntities qasEntities = new QASModel.QASEntities();
QASModel.User authenticatedUserEntity = (from u in qasEntities.Users
where u.ID.Equals(authenticatedUserId)
select u).FirstOrDefault();
// Get the authenticated user usergroups
var usergroup = authenticatedUserEntity.UserGroups.FirstOrDefault();
// Get all testcases of all user group the authenticated user is associated to
var allTestcases = usergroup.TestCases;
// Get the authenticated user projects based on it's usergroup(s)
var authenticatedUserProjects = usergroup.Projects;
authentiatedUserProjects 仅返回 1 个项目,其中用户链接到 2 个项目。 allTestcases 没有返回任何结果,尽管 TestCases 中有大约 8 个条目与与用户所属的同一 UserGroup 之一相关联的项目相关联。
谢谢
I'm struggling to get a collection of records using L2E. Here's the model view:
http://pascalc.nougen.com/stuffs/aspnet_linq_model2.png
I have a user identifier, which is associated to 1 or many UserGroup which themselves are linked to TestCase. I would like to get all TestCases of all groups the user id X is associated to.
I also notice that I don't get all Project for users that are associated to 2 (or more).
Here's how I do so far:
QASModel.QASEntities qasEntities = new QASModel.QASEntities();
QASModel.User authenticatedUserEntity = (from u in qasEntities.Users
where u.ID.Equals(authenticatedUserId)
select u).FirstOrDefault();
// Get the authenticated user usergroups
var usergroup = authenticatedUserEntity.UserGroups.FirstOrDefault();
// Get all testcases of all user group the authenticated user is associated to
var allTestcases = usergroup.TestCases;
// Get the authenticated user projects based on it's usergroup(s)
var authenticatedUserProjects = usergroup.Projects;
authenticatedUserProjects give back only 1 project, where the user is linked to 2 projects.
And allTestcases gives back no result, although there are about 8 entries in TestCases associated to a project associated to one of the same UserGroup the user belongs to.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的问题在于这一行:
你的代码不应该获取该
User
的所有UserGroups
吗?上面的行只会返回 1 个UserGroup
,也就是说,如果用户属于超过 1 个UserGroup
,则不会返回第二个。要纠正这个问题:
I think your problem is in this line:
Shouldn't your code get all
UserGroups
of thatUser
? The above line will return only 1UserGroup
, this is, if the user belongs to more than 1UserGroup
the 2nd one won't be returned.To correct this:
可能是错误的,因为您需要与该用户关联的所有用户组。
在任何情况下,您都可以轻松地将查询恢复为这样工作,
这样您就不必执行多个查询来获取所有测试用例,每个 FirstOrDefault 或 ToList 或 ForEach 将实际执行查询。
probably is wrong, since you want all usergroups the user is associated with.
In any case you can easily revert the query to work like this
This way you won't have to execute multiple queries to get all test cases, each FirstOrDefault or ToList or ForEach will actually execute the query.