1

我正在努力使用 L2E 获取记录集合。这是模型视图: http: //pascalc.nougen.com/stuffs/aspnet_linq_model2.png

我有一个用户标识符,它与 1 个或多个用户组相关联,这些用户组本身链接到 TestCase。我想获取与用户 id X 关联的所有组的所有 TestCases。

我还注意到,对于与 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;

authenticatedUserProjects 仅返回 1 个项目,其中用户链接到 2 个项目。并且 allTestcases 没有返回任何结果,尽管 TestCases 中有大约 8 个条目与与用户所属的同一用户组之一相关联的项目相关联。

谢谢

4

2 回答 2

1

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);

    ...
}
于 2011-05-30T20:51:54.130 回答
0
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.

于 2011-05-30T20:54:34.697 回答