LINQ, "Argument types do not match" error, what does it mean, how do I address it?

Biff MaGriff picture Biff MaGriff · Jun 9, 2010 · Viewed 15.4k times · Source

I'm new to linq and I'm trying to databind to an anonymous type.

I'm using SubSonic 3.0 as my DAL.

I'm doing a select from 2 tables like so

        var myDeal = (from u in db.Users
                  select new
                      {
                          UserID = u.UserID,
                          UserRoleID = (from ur in u.UserRoles where u.UserRoleID == ur.UserRoleID select ur).FirstOrDefault().UserRoleID
                      });
    foreach (var v in myDeal) //dies first time here
    {
    }

Then when I databind or try to iterate through the collection I get the "Argument types do not match" error during run time.

I'm not sure what is going on here.

Answer

Patrick picture Patrick · Jun 9, 2010

Is one of the ID fields nullable?

If so, you need to access the .Value property

var myDeal = (from u in db.Users 
        join ur in     
              select new 
                   { 
                     UserID = u.UserID, 
                     UserRoleID = (from ur in u.UserRoles where u.UserRoleID.Value equals ur.UserRoleID select ur).FirstOrDefault().UserRoleID 
                    });

You could also try something like this:

var myDeal = (from u in db.Users
         join ur in db.UserRoles
        on new {ID = u.UserRoleID.value} equals new {ID = ur.UserRoleID} into tempRoles
        from roles in tempRoles.DefaultIfEmpty()
        select new
        {
           UserID = u.UserID, 
                 UserRoleID = roles.UserRoleID
        }); 

You use tempRoles to execute a left outer join. So if there is no role assigned, you still get the userID.

I haven't tested this, this is just off the top of my head.

Good Luck,

Patrick.