score:10
after a lot of struggle i found solution to my question. i want to achieve this sql query
select * from courses where courseid in (1, 2, 3, 4)
using linq to entities, but i want to pass in(1,2,3,4) list dynamically to linq query. i created an extension class for that purpose.
public static class linqextensions
{
public static expression<func<t, bool>> false<t>() { return f => false; }
public static expression<func<t, bool>> in<t, tvalue>(this expression<func<t, bool>> predicate,string propertyname, list<tvalue> values)
{
var param = predicate.parameters.single();
memberexpression property = expression.propertyorfield(param, propertyname);
var micontain = typeof(list<tvalue>).getmethod("contains");
var mc = expression.call(expression.constant(values), micontain, property);
return expression.lambda<func<t, bool>>(mc, param);
}
}
use of linqextensions
var pred = linqextensions.false<course>(); //you can chain in function like linqextensions.false<course>().in<course, int>("courseid", inlist);
var inlist= new list<int>(){1, 2, 3}; //keep in mind the list must be of same type of the property that will be compared with. in my case courseid is integer so the in list have integer values
pred =pred.in<course, int>("courseid", inlist); //tvalue is int. as courseid is of type int.
var data = myentities.courses.where(pred);
i hope this might be beneficial for some one
score:0
have you seen the type of
var courselist = dbentities.courses.where(c => ids.contains(c.courseid)))
above statement would not return actual list of courses. the query is not executed yet. it just returns iquereable. the query is executed when you actually call .tolist() method on it
so, your solution is..
create array of ids using for loop and then simply run the below query
var courselist = dbentities.courses.where(c => ids.contains(c.courseid))).tolist()
Source: stackoverflow.com
Related Query
- Dynamic linq query expression tree for sql IN clause using Entity framework
- Strange Exception thrown using Dynamic Linq Entity Framework Query
- How to pass an Expression into Entity Framework LINQ query OrderBy clause
- Pull data from multiple tables in one SQL query using LINQ and Entity Framework (Core)
- Using Linq to query Entity Framework with Where clause and many-to-many relation
- LINQ query help: searching for data in a Many to Many using Entity Framework
- LINQ dynamic query for entity framework
- Is there a Linq Entity Framework query for using an inner join AND an IN() statement?
- Searching for multiple strings using single database query with entity framework and LINQ
- LINQ Dynamic Query using Expression Tree
- C# Linq Lambda Expression for Entity Framework Query Passing Custom Expression to Where Condition
- dynamic query in where clause for linq using c#
- Converting a Linq expression tree that relies on SqlMethods.Like() for use with the Entity Framework
- Convert a given SQL query into linq for Entity Framework
- SQL query to LINQ expression - Entity Core Framework 3 + SQL Server
- How do I edit this LINQ query for nested classes using Entity Framework
- Using Expression Tree in LINQ to Entity Framework Core
- Proper Linq Query for objects with many to many relation ship generated with code first entity framework
- Entity Framework dynamic linq where from generic source with dynamic where clause
- Translating SQL Query to Linq for use with Entity Framework
- Entity Framework EF Query using LINQ for related Entities - Getting Customers with their orders from specific period of time only
- How to avoid Query Plan re-compilation when using IEnumerable.Contains in Entity Framework LINQ queries?
- Steps for a beginner to run very basic linq to sql query using Linqpad
- Cast Entity to Implemented Interface in a Generic Method Using LINQ for Entity Framework
- Deleting multiple records with Entity Framework using a single LINQ query
- Using Function in Select Clause of Entity Framework Query
- ThenInclude not working for an Entity Framework LINQ query
- Dynamic query using LINQ to SQL
- How can i write SQL update query with where clause in Entity Framework in C#
- Retrieve single Entity Framework entities using a LINQ query or GetObjectKey?
More Query from same tag
- SQL query to linq (with/as, update)
- ParseLambda method throws an exception
- Converting iQueryable to IEnumerable
- Linq - How to turn IQueryable<IEnumerable> into IQueryable
- Simple Linq to XML question
- Using custom methods in LINQ query
- Selecting multiple items from the Custom list
- Linq query order by giving me issues
- Linq multiple join conditions using extra variables
- How to filter list of objects with child collection by list of strings
- LINQ vs foreach vs for performance test results
- Linq Select IList
- Create intervals of data with LINQ
- Entity Framework IQueryable - specific date
- Linq expression with GroupBy
- Will bad things happen to me if I name my arrays, collections, lists, enumerables, etc. just the plural of what they contain?
- How does LINQ .distinct method sort?
- How to query Anonymous Type collection?
- LINQ: How to select item elements where child node satisfies condition?
- From Linq To List Using Extension Method
- Converting a Query to LINQ
- question about linq select and ToList()
- LINQ Group & Sum on a list of Models where a property is another model
- LINQ Select First
- Execute OrderBy with parameter
- Update values of a list using Linq
- how to translate linq groupjoin to lambda groupjoin
- How to find a specific element in a List
- Counting Word Frequency (most significant words) in a String, excluding keywords
- Initialize a Linq to Sql object via business logic