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();
Related
This question already has answers here:
How to do joins in LINQ on multiple fields in single join
(13 answers)
Closed 2 years ago.
This is My Query in Linq
VAR x = FROM n IN _db.tbl_maint_activity_material_details
JOIN availablity IN _db.tbl_maint_item_availability ON n.mamd_lotno equals availablity.mia_lot_no && n. ////
Here im not able to countine my Code Please Help me
This is My SQL query
SELECT *
FROM tbl_maint_activity_material_details AS _material
JOIN tbl_maint_item_availability AS availablity
ON _material.mamd_maint_item_id = availablity.mia_maint_item_id
AND _material.mamd_lotno = availablity.mia_lot_no
WHERE _material.mamd_status = 24
To accomplish that, you have to build an object and use equals.
Something like:
on new { mia_lot_no = n.mamd_lotno, mamd_lotno = n.mamd_lotno } equals new { mia_lot_no = availablity.mia_lot_no, mamd_lotno = ... }
So that the equals will result true if the whole object is equal.
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.
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:
Get result function in LINQ without translate to store expression
(2 answers)
Problem with converting int to string in Linq to entities
(15 answers)
Closed 5 years ago.
Convert string to double using LINQ query and get data
data1 => string type
data2 => double type
Query :
var query = get to db
query= query.Where(W => double.Parse(W.data1) >= W.data2).ToList();
Error :
"LINQ to Entities does not recognize the method 'Double
ToDouble(System.String)' method, and this method cannot be translated
into a store expression."
This question already has answers here:
Linq to SQL how to do "where [column] in (list of values)"
(6 answers)
Closed 6 years ago.
I have an entity framework query as shown below. Here I am trying to find entities for which there is no fax information for primary and secondary users. I am using two person_role_cd conditions using “OR”.
What is the best LINQ way to make the same logic with something like SQL “IN” condition? For example, p.person_role_cd IN (“Primary”, “Secondary” )
query = query
.Where(l => l.loan_documents
.FirstOrDefault(d => d.document_type_cd == “Application”)
.loan_persons.Any(p =>
(
p.fax_no != null
&&
(
p.person_role_cd == “Primary”
||
p.person_role_cd == “Secondary”
)
)
) == false
);
First create an array with all strings you want to check against.
var targetList = new[] {"Primary", "Secondary"};
Then Replace your OR condition with
targetList.Contains(p.person_role_cd);
Caveat: Doing stuff like this causes problems with Linq to Entities
That said, the first thing that comes to mind is use a List with Contains (which is basically what the provided SQL does)
List<string> roles = new List<string>() { "Primary", "Secondary" };
query = query
.Where(l => l.loan_documents
.FirstOrDefault(d => d.document_type_cd == “Application”)
.loan_persons.Any(p => p.fax_no != null && roles.Contains(p.person_role_cd));
Couldn't quite tell where your negation belongs, left as exercise to the reader. The reason it may not work with Linq to Entities is that translating List.Contains (especially off of a local variable) to SQL is the kind of task it likes to give up on. If your types are literally two items, I would just keep the condition as is (apart from considering an enum for my types instead of strings)