【问题标题】:getting error in joining dictionary with table将字典与表连接时出错
【发布时间】:2015-10-05 11:15:02
【问题描述】:

我想从带有基于公共字段的字典的表中进行连接查询,我的查询是:

var query = from c in db.Exp
join d in lstUniprotDic on c.UniID equals d.Key
select new
{              
    c.UniID,                        
    IdentityPercent=d.Value.ToString(),
    c.PrId,
    c.SpotNo
}

但我收到以下错误

不能在 LINQ to SQL 实现的查询运算符中使用本地序列,但 Contains() 运算符除外。

【问题讨论】:

    标签: c# sql sql-server linq dictionary


    【解决方案1】:

    这几乎说明了一切。您不能在 LINQ to SQL 查询中使用字典,除非使用 Contains

    解决方案:

    (from c in db.Exp where lstUniprotDic.Keys.Contains(c.UniID) select c).AsEnumerable()
    join d in lstUniprotDic on c.UniID equals d.Key
    select new
    {              
        c.UniID,                        
        IdentityPercent=d.Value.ToString(),
        c.PrId,
        c.SpotNo
    }
    

    我不确定在 LINQ to SQL 查询中使用 lstUniprotDic.Keys 是否真的有效。
    如果没有,请尝试使用此代码:

    var ids = lstUniprotDic.Keys.ToArray();
    (from c in db.Exp where ids.Contains(c.UniID) select c).AsEnumerable()
    join d in lstUniprotDic on c.UniID equals d.Key
    select new
    {              
        c.UniID,                        
        IdentityPercent=d.Value.ToString(),
        c.PrId,
        c.SpotNo
    }
    

    【讨论】:

    • 对不起,丹尼尔,但它会产生另一个错误,您以 code(来自 code 并以 code 结束)。AsEnumerable()code,VS 说查询必须发送以选择结束短语。
    • @user3616494:我理解你的评论,虽然它很奇怪。请查看固定版本。
    猜你喜欢
    • 2021-10-20
    • 2021-08-15
    • 2018-08-19
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 2021-01-26
    相关资源
    最近更新 更多