LINQ,“参数类型不匹配”错误,这是什么意思,我该如何解决?

发布于 2024-09-04 12:58:51 字数 561 浏览 4 评论 0原文

我是 linq 的新手,我正在尝试将数据绑定到匿名类型。

我使用 SubSonic 3.0 作为我的 DAL。

我正在从 2 个表中进行选择,如下所示

        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
    {
    }

然后当我进行数据绑定或尝试迭代集合时,我在运行时收到“参数类型不匹配”错误。

我不确定这里发生了什么事。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃扇骨 2024-09-11 12:58:51

ID 字段之一可以为空吗?

如果是这样,您需要访问 .Value 属性

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

您也可以尝试如下操作:

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

您使用 tempRoles 执行左外连接。因此,如果没有分配角色,您仍然可以获得用户 ID。

我还没有测试过这个,这只是我的想法。

祝你好运,

帕特里克。

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.

尐籹人 2024-09-11 12:58:51

“参数类型不匹配”错误通常是由我们要绑定的属性与其元素之间的类型不匹配引起的。

RadGridSparkline 中的示例,当我们想要绑定其项目源时

“值”类型必须是双精度型。

如果我们的 Value 属性定义如下
公共小数合约价值
{ 放;得到;将

显示此错误

"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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文