1

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

user706058
  • 415
  • 3
  • 11
  • 23
  • The trick was to cast the definition as a list: `List allTestCases = new EntityCollection().ToList();` then concat: `allTestCases = allTestCases.Concat(usergroup.TestCases).ToList();` – user706058 May 30 '11 at 22:30

2 Answers2

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

    ...
}
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • Thanks Leniel, the only issue I have now is that when a user has more than 1 usergroup associated, the variables (allTestcases and authenticatedUserProjects) get 'reset' each pass in the foreach(). How can I 'contact' each result in each pass? – user706058 May 30 '11 at 21:19
  • It complains it cannot convert from IEnumerable to List on the concat(). Thanks – user706058 May 30 '11 at 22:25
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.

AD.Net
  • 13,352
  • 2
  • 28
  • 47
  • Thanks AD.Net but I don't have the property "UserId" in "tc.UserGroup.UserId" I tried `where tc.UserGroup.Users.Select(x => x.ID.Equals(authenticatedUserId))` but that not good. I'm new to L2E. Thanks – user706058 May 30 '11 at 21:29
  • Well, of course I don't have the context right now with me, so I might have missed it. From what I see, it might be tc.UserGroup.Users.Any(u=>u.ID.Equals(authenticatedUserId) OR tc.UserGroup.Users.UserId == authenticatedId (if each group has only one user) BTW: there was no need for -ve. Thanks. – AD.Net May 30 '11 at 21:35
  • Thanks for the comment AD.Net. BTW, I did not do that. – user706058 May 31 '11 at 08:19