LINQ查询不断抛出"无法建立System.Object类型的恒定值....",为什么呢? [英] Linq Query keeps throwing "Unable to create a constant value of type System.Object....", Why?

查看:187
本文介绍了LINQ查询不断抛出"无法建立System.Object类型的恒定值....",为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码示例:

private void loadCustomer(int custIdToQuery) 
    {
        var dbContext = new SampleDB();
        try
        {
            var customerContext = from t in dbContext.tblCustomers      // keeps throwing:
                                   where t.CustID.Equals(custIdToQuery) // Unable to create a constant value of type 'System.Object'. 
                                   select new                           // Only primitive types ('such as Int32, String, and Guid') 
                                   {                                    // are supported in this context.
                                       branchId = t.CustomerBranchID,   //
                                       branchName = t.BranchName        //
                                   };                                   //

            if (customerContext.ToList().Count() < 1) //Already Tried customerContext.Any()
            {
                lstbCustomers.DataSource = customerContext;
                lstbCustomers.DisplayMember = "branchName";
                lstbCustomers.ValueMember = "branchId";
            }
            else
            {
                lstbCustomers.Items.Add("There are no branches defined for the selected customer.");
                lstbCustomers.Refresh();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            dbContext.Dispose();
        }
    }



我无法明白我做错了。我不断收到无法创建类型'System.Object的恒定值,只有原始类型('如的Int32,字符串和GUID')在这方面的支持。

推荐答案

使用的==而不是等于:

Use == instead of Equals:

where t.CustID == custIdToQuery

如果该类型是不正确的,你可能会发现,这并未T编译。

If the types are incorrect you may find that this doesn't compile.

这篇关于LINQ查询不断抛出&QUOT;无法建立System.Object类型的恒定值....&QUOT;,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆