【问题标题】:Doing a IN statement with LINQ [duplicate]使用 LINQ 执行 IN 语句 [重复]
【发布时间】:2012-12-05 23:56:36
【问题描述】:

可能重复:
Use dictionary inside linq query

如何使用 LINQ 生成 IN 语句?

我有什么

    var t = from i in emails
            where i.Credencials_Id == agenciaid
            select new
            {
                i.Alias,
                i.Name,
                i.Description,
                i.Comentas,
                i.DateHour,
                i.Imovel_Id,
                i.Email,
                i.EmailCaptador
            };

我想要什么(类似这样的)

    var t = from i in emails
            where i.Credencials_Id IN (agenciaid)
            select new
            {
                i.Alias,
                i.Name,
                i.Description,
                i.Comentas,
                i.DateHour,
                i.Imovel_Id,
                i.Email,
                i.EmailCaptador
            };

int imovel = Convert.ToInt32(t.First().Imovel_Id);

【问题讨论】:

  • 如果使用Linq2Objects:where agenciaid.Contains(i.Credencials_Id),Linq2Sql会遇到参数限制。

标签: linq entity-framework


【解决方案1】:

当您使用 LINQ to Objects 时,您可以使用:

from i in emails
where agenciaid.Contains(i.Credencials_Id)
select new
{
    i.Alias,
    i.Name,
    i.Description,
    i.Comentas,
    i.DateHour,
    i.Imovel_Id,
    i.Email,
    i.EmailCaptador
};

【讨论】:

  • 我收到以下错误System.NotSupportedException: LINQ to Entities does not recognize the method 'Boolean Contains (System.Object)' which can not be converted to an expression Storage
  • 在这种情况下,您没有使用 LINQ to Objects。我将您的问题标记为重复。有关如何执行此操作,请参阅链接问题的已接受答案。
  • 但在我的情况下如何调用WhereIN 方法?
  • EF 支持 contains 关键字。 agenciaid 需要是某种集合,int[] 或 List 等...
  • TGnat agenciaid 是ArrayList,但发生了我上面所说的错误。
猜你喜欢
  • 2017-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多