如何查询linq列表在哪里条件? [英] How to query linq for list in where condition?

查看:76
本文介绍了如何查询linq列表在哪里条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的方法

i have method like this

public List<cil> Accounts( List<string> CL)
{
List<cil> cil = new List<cil>();
cil =(from CI in context.CIs join V1 in context.V1 on CI.Id equals V1.Id where CL.Contains(CI.ID)
select new CIL()
{
Name = V1.Name
}).ToList();
}
return cil;
}

我的 方法参数将返回 CL列表,如下所示

1111

2222

在我的数据库中有

CI 表有

< b> ID

第1行:1111

第2行:2222



现在查询cil返回null

实际上它假设返回

1111

2222


incase 我的方法参数将返回CL列表,如下面只有一行

1111

返回一行

cil返回 1111

注意:所有相同的身份证明在 条件中检查等于



我的尝试:



public列表与LT; CIL>帐户(列表<字符串> CL

{

列表< cil> cil = new List< cil>();

cil =(来自 CI in context.CIs 加入V1 in context.V1 on CI.Id等于V1.Id其中< b> CL.Contains(CI.ID)

选择新的CIL()

{

Name = V1.Name

})。ToList();

}

返回cil;

}



任何答案都将不胜感激。

my method parameter will return List of CL like below
1111
2222
In my db having
CI table having
ID
Row1: 1111
Row2: 2222


now above query cil returning null
Actually it suppose to return
1111
2222

incase my method parameter will return List of CL like below only one row
1111
its returning one row
that is cil returning 1111
Note: All same Id checked in on condition equals

What I have tried:

public List<cil> Accounts( List<string> CL)
{
List<cil> cil = new List<cil>();
cil =(from CI in context.CIs join V1 in context.V1 on CI.Id equals V1.Id where CL.Contains(CI.ID)
select new CIL()
{
Name = V1.Name
}).ToList();
}
return cil;
}

Any Answers would be appreciated.

推荐答案

Have you tried using the IEnumerable extensions instead?  I find it much easier:

<pre lang="c#">public List<cil> Accounts( List<string> CL){

  var cil= context.CIs
               .Join(
                  context.V1,
                  ci=>ci.Id,
                  v1=>v1.Id
                  (ci,v1)=>new{ci, v1})
               .Where(a => CL.contains(a.ci.ID))
               .Select(a=>a.v1.Name)
               .ToList();
               
}





然后您可以分解查询以查看错误。



另外,如果那个上下文是实体框架,那么在你调用ToList()之前不会运行查询。



Take看看这个:



You can then break down the query to see what's wrong.

Also, if that "context" is entity framework then the query is not run until you call ToList().

Take a look at this:

public List<cil> Accounts( List<string> CL){

  var query= context.CIs
               .Join(
                  context.V1,
                  ci=>ci.Id,
                  v1=>v1.Id
                  (ci,v1)=>new{ci, v1})
               .Where(a => CL.contains(a.ci.ID))
               .Select(a=>a.v1.Name);
  
  var queryString = query.ToString();

  return query.ToList(); 
}







该查询字符串将是将在DB上运行的SQL(没有变量)。看看你的逻辑是否在某处出错。也许连接错误或数据不符合您的期望?



希望有帮助

Andy




That querystring will be the SQL that will be run on the DB (without the variables). Take a look and see if your logic is wrong somewhere. Maybe a join is wrong or the data isn't as you expect it ?

Hope that helps
Andy


这篇关于如何查询linq列表在哪里条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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