我有这样的方法
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列表在哪里条件?
发布评论