Linq to Entities:无法使集合查询工作

发布于 2024-11-10 16:34:11 字数 1119 浏览 6 评论 0原文

我正在努力使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朦胧时间 2024-11-17 16:34:11

我认为你的问题在于这一行:

var usergroup = authenticatedUserEntity.UserGroups.FirstOrDefault();

你的代码不应该获取该User的所有UserGroups吗?上面的行只会返回 1 个 UserGroup,也就是说,如果用户属于超过 1 个 UserGroup,则不会返回第二个。

要纠正这个问题:

var userTestCases = new List<TestCase>();
var userProjects =  new List<Project>();

foreach(UserGroup ug in authenticatedUserEntity.UserGroups)
{
    userTestCases = userTestCases.Concat(ug.TestCases);

    // Get the authenticated user projects based on it's usergroup(s)
    userProjects = userProjects.Concat(ug.Projects);

    ...
}

I think your problem is in this line:

var usergroup = authenticatedUserEntity.UserGroups.FirstOrDefault();

Shouldn't your code get all UserGroups of that User? The above line will return only 1 UserGroup, this is, if the user belongs to more than 1 UserGroup the 2nd one won't be returned.

To correct this:

var userTestCases = new List<TestCase>();
var userProjects =  new List<Project>();

foreach(UserGroup ug in authenticatedUserEntity.UserGroups)
{
    userTestCases = userTestCases.Concat(ug.TestCases);

    // Get the authenticated user projects based on it's usergroup(s)
    userProjects = userProjects.Concat(ug.Projects);

    ...
}
月寒剑心 2024-11-17 16:34:11
var usergroup = authenticatedUserEntity.UserGroups.FirstOrDefault();

可能是错误的,因为您需要与该用户关联的所有用户组。
在任何情况下,您都可以轻松地将查询恢复为这样工作,

var testcases = from tc in new QASModel.QASEntities().TestCases
where tc.UserGroup.UserId == USERID
select tc

这样您就不必执行多个查询来获取所有测试用例,每个 FirstOrDefault 或 ToList 或 ForEach 将实际执行查询。

var usergroup = authenticatedUserEntity.UserGroups.FirstOrDefault();

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

var testcases = from tc in new QASModel.QASEntities().TestCases
where tc.UserGroup.UserId == USERID
select tc

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文