如何查询linq列表在哪里条件?

编程入门 行业动态 更新时间:2024-10-26 04:19:33
本文介绍了如何查询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 表有 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其中 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列表在哪里条件?

本文发布于:2023-11-16 21:54:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607622.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   列表   linq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!