Click here to Skip to main content
15,891,645 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have method like this
C#
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;
}
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.
Posted
Updated 8-Oct-18 1:02am
v7

1 solution

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();
               
}


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:
C#
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(); 
}



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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900