I'm not entirely certain that this is what you're looking for, but I'm going to give it a shot:

Try chaining your where clauses in Linq to SQL, and you may get a better result:

List<string> outputRoleUserList = 
           from rls in roleUserList
           from mis in missingRoleUserList
           where rls.RoleID != mis.RoleID
           where rls.UserID != mis.UserID
           select rls.UserID + ",\"" + rls.RoleID

This will actually generate SQL as follows:

rls.RoleId != mis.UserID AND rls.UserId != mis.UserID

However, you have already forced execution on roleUserList and missingRoleUserList, so what you're using in the third Linq statement is not really Linq to SQL but rather Linq to Objects, if I'm reading this correctly.

I'd be curious to see some additional information or clarification and then maybe I'll understand better what's going on!

EDIT: I realized another possibility, it's possible that the object.UserID or object.RoleID is throwing an internal NullPointerException and failing out because one of those values came back null. You could possibly solve this with the following:

List<string> outputRoleUserLIst2=roleUserList
    .Where(x => x != null && x.UserID != null && x.RoleID != null && missingRoleUserList
        .Where(y => y != null && y.UserID != null && y.RoleID != null && y.RoleID!=x.RoleID && y.UserID!=x.UserID)
    .Select(x => x.UserID + ",\"" + x.RoleID).Distinct().ToList();

This is not pretty, and this is the other Linq syntax (with which I am more comfortable) but hopefully you understand what I am going for here. I'd be curious to know what would happen if you dropped this into your program (If I've guessed all of your meanings correctly!). I'll look back in a bit to see if you have added any information!

Related Articles