This question already has answers here:
C# Linq where clause as a variable
(5 answers)
Closed 2 years ago.
In LINQ, is it possible to pass .Where conditions as parameter?
IList<Object> obj = persons
.Where(p => p.Text.Contains("x") || p.Text.Contains("y"))
.ToList();
So that more than one dynamic conditions
The single line you have posted is equivalent to the following:
bool filter( Person p )
{
return p.Text.Contains( "x" ) || p.Text.Contains( "y" );
}
IList<Object> obj = persons.Where( filter ).ToList();
I hope this answers your question.
Related
This question already has answers here:
Multiple "order by" in LINQ
(7 answers)
Closed 3 years ago.
I'm trying to order by WeekId, then Order in my SQL table (end result should have workouts together by id, then ordered by the order specified), yet it is giving the wrong order. Is there something wrong with my LINQ statement?
private List<Workout> GetWorkouts(int id)
{
return new OPPDBContext().Workouts
.Where(p=>p.ClientId == id).OrderBy(p => p.Order).OrderBy(p => p.WeekId).ToList();
}
The table:
The results:
Expected results:
Lat Pulldowns
Squats
Lat Pulldowns
Squats
Reverse Lunges
That's because the second .OrderBy replaces the first .OrderBy (you are sorting by ClientId, and then effectively discard that to sort by WeekId).
You need to use .OrderBy(...).ThenBy(...) instead:
return new OPPDBContext().Workouts.Where(p=>p.ClientId == id).OrderBy(p => p.Order).ThenBy(p => p.WeekId).ToList();
OrderBy docs
ThenBy docs
This question already has answers here:
Linq Convert.ToInt32 in Query
(2 answers)
Closed 4 years ago.
Can anyone help me with the following error?
LINQ to Entities does not recognize the method 'Int32
Int32(System.String)' method, and this method cannot be translated
into a store expression.
Below is my code, I am trying in several ways to fix this error, but I have not been successful:
public IEnumerable<Dia1> GetPendenciasByUser(int centroId)
{
var query = Db.Dia1S
.Join(Db.Cadastros, dia1 => dia1.PatientId, cad => cad.PatientId, (dia1, cad) => new { dia1, cad })
.Join(Db.Randomizacao, dia1 => dia1.dia1.PatientId, rand => rand.PatientId, (dia1, rand) => new { dia1, rand })
.Where(s => s.dia1.dia1.dtd1 == null ? (Convert.ToInt32(DateTime.Now - s.rand.RandomizacaoData)) > 1 : (Convert.ToInt32(Convert.ToDateTime(s.dia1.dia1.dtd1) - s.rand.RandomizacaoData)) > 1 )
.Select(s => s.dia1.dia1)
.ToList();
return query;
}
The error message is clear, LINQ doesn't know how to convert the Convert.ToInt32() function to SQL. You can either use direct casting like this:
(int)(DateTime.Now - s.rand.RandomizacaoData)
Or you'll have to execute the query and get the data, then convert it in memory using Convert.ToInt32() as you wish.
This question already has answers here:
C# Lambda expressions: Why should I use them?
(17 answers)
Closed 5 years ago.
r => r.Item1 == dFName
Its part of this line
dFName = PropertyUtil.GetName<kUpdate>(r => r.Resolution);
fld2Lup = map.DTField2LookupMap[domType].First(r => r.Item1 == dFName );
That is essentialy..
Return the First item of the "DTField2LookupMap[domType]" collection where the items property "Item1" is equal to dfName
this is a lambda expression: https://msdn.microsoft.com/en-us/library/bb397687.aspx
This question already has answers here:
Where IN clause in LINQ [duplicate]
(8 answers)
Linq to Entities - SQL "IN" clause
(9 answers)
Closed 8 years ago.
I am trying to mimic an IN() clause commonly used in SQL and use it in my LINQ statement.
I see that there is an overload for Contains() that takes an IEnumerable collection. I have tried passing in ILIST and Dictionary but neither is correct.
How do I accomplish this?
Thanks, much appreciated.
SQL
select
oec.OnlineEducationCourseId,
oec.CourseTitle,COUNT(oec.CourseTitle) as CourseCount
from OnlineEducationRegistration as oer
join OnlineEducationCourse oec on oec.OnlineEducationCourseId = oer.OnlineEducationCourseId
where oer.ClubId IN('K20','B67','P89')
and DateCompleted between '2013-01-01' and '2014-01-01'
group by oec.CourseTitle,oec.OnlineEducationCourseId
Same SQL query in LINQ
var r = (from oer in db.OnlineEducationRegistrations
join oec in db.OnlineEducationCourses on oer.OnlineEducationCourseId equals
oec.OnlineEducationCourseId
where (oer.ClubId.Contains(some IEnumerable collection here) && oer.DateCompleted >= start.Date && oer.DateCompleted <= end.Date)
group new { oec, oer } by new { oec.CourseTitle, oec.OnlineEducationCourseId }).ToList();
This question already has answers here:
Linq Contains method for a object
(4 answers)
Closed 10 years ago.
I have an IEnumerable<Project>
I want to know if this list has any element Project.ID == someID.
Is there a way to do that?
Yes, you want to use the Any method (documentation).
IEnumerable<Project> projects = SomeMethodReturningProjects();
if(projects.Any(p => p.ID == someID))
{
//Do something...
}
You can use the Any() extension method.
var hasAny = projectList.Any(proj => proj.ID == someID);
Or, if you want to get that record, you can use FirstOrDefault():
var matchedProject = projectList.FirstOrDefault(proj => proj.ID == someID);
This will return null if it finds nothing that matches, but will pull the whole object if it does find it.
Using
projects.Any(p => p.ID == someID)
returns true (a boolean) if the predicate matched for any element.
Yes, use the Any extension method:
list.Any(p => p.ID == someID);