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

2

5

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.

Idealist answered 9/6, 2010 at 16:20 Comment(4)
Does the query still fail if you just iterate over it fetching the results, i.e. without the data binding?Delitescence
Yeah I put a foreach right after and it dies on the first time seeing it.Idealist
If you check in the debugger what's the type of myDeal?Picador
System.Linq.IQueryable<<>f__AnonymousType0<int,int>> {SubSonic.Linq.Structure.Query<<>f__AnonymousType0<int,int>>}Idealist
A
6

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.

Aquarius answered 9/6, 2010 at 16:54 Comment(1)
I couldn't get it to work :( reverting to active record on this one. Thanks anyways.Idealist
M
3

"Argument types do not match" error is usually caused by the un-matching type between the property we would like to bind and its element.

Example in RadGridSparkline, when we would like to bind its item source

"Value" type must be double.

If we have the Value property defines as below public decimal ContractValue { set; get; }

This error will be displayed

Man answered 29/7, 2011 at 3:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.