score:3
honestly, this sounds like a case where you would want to make use of a predicatebuilder
details on that here
in essence though it would leave you doing something like
var predicate = prodicatebuilder.false<somedto>();
foreach (var requestinfo in requestinfos)
{
predicate = predicate.or(i=> i.id1 == requestinfo.id1 && i.id2 == requestinfo.id2);
}
and then within your where clause you just pass in the predicate.
equally, it also details how to use expressions directly over at that link, if you wanted to do that as opposed to adding the predicatebuilder code to your work.
score:0
if you want the really simple answer:
_context.sometable.where(x => x.id1 == 1 && (x.id2 == 2 || x.id2 == 3));
however i'm assuming your case probably won't be that simple, you can create a list of ids that it should match and use like so:
var id1s = new list<int>{ 1 };
var id2s = new list<int>{ 2, 3 };
_context.sometable.where(x => id1s.contains(x.id1) && id2s.contains(x.id2));
score:0
i think this could may help you
var emails = _usermanager.users
.where(user => user.customerid == null)
.select(user => user.email) // extract the emails from users
.tolist();
var customers = _applicationrepository.getcustomers()
.where(customer => emails.contains(customer.email)) // the contains method carry the in logic when translated to sql script
.tolist();
score:0
if you're using value tuples this would be the answer :
list<(int id1, int id2,string somecolumn)> your_list= new list<(int id1, int id2, string somecolumn)>();
var your_data= your_list.where(x => (x.id1 == 1 && x.id2 == 2) || (x.id1 == 1 && x.id2 == 3));
score:0
score:1
you should be able to build it like this
var requestinfos = new list<(int id1, int id2)>();
requestinfos.add((1, 2));
requestinfos.add((1, 3));
var sometable = new list<(int id1, int id2, string somecolumn)>();
sometable.add((1, 2, "12"));
sometable.add((2, 2, "22"));
sometable.add((1, 3, "13"));
var parameter = expression.parameter(typeof((int id1, int id2, string somecolumn)));
expression body = expression.constant(true);
foreach (var requestinfo in requestinfos)
{
var tableid1 = expression.makememberaccess(parameter, typeof((int id1, int id2, string somecolumn)).getmember("item1")[0]);
var tableid2 = expression.makememberaccess(parameter, typeof((int id1, int id2, string somecolumn)).getmember("item2")[0]);
var paramid1 = expression.makememberaccess(expression.constant(requestinfo), typeof((int id1, int id2)).getmember("item1")[0]);
var paramid2 = expression.makememberaccess(expression.constant(requestinfo), typeof((int id1, int id2)).getmember("item2")[0]);
var and = expression.and(expression.equal(paramid1, tableid1), expression.equal(paramid2, tableid2));
body = expression.orelse(body, and);
expression<func<(int id1, int id2, string somecolumn), bool>> buf = i => i.id1 == requestinfo.id1 && i.id2 == requestinfo.id2;
}
var func = expression.lambda<func<(int id1, int id2, string somecolumn), bool>>(body, new[] {parameter});
var query = sometable.asqueryable().where(func);
still this is quite complicated and gets out of hand quickly. the predicate builder (also mentioned by gibbon's answer) should be a more scalable approach
Source: stackoverflow.com
Related Query
- How to create where by list of complex clauses?
- How to create new list from list of list where elements are in new list are in alternative order?
- How can I compare two Lists and create another list where the match?
- How can I create list of nested classes where a certain property of each nested class is true?
- How to Create a Linq Query with Multiple Conditional Where Clauses
- in C#, how do I order items in a list where the "largest" values are in the middle of the list
- LINQ Where clause with Contains where the list has complex object
- How can I create a conditional where clause using LINQ
- How to create a complex type from a stored procedure that uses exec()?
- How to create a comma delimited string from distinct list of values using LINQ?
- How do I create a where condition on a sub table in LINQ
- How to create a concatenated string from a List of Objects in VB.Net 3.5
- How to create a RavenDB index that returns a list of strings?
- How to create a list from filtering 2 lists with linq to object
- Chain together multiple complex WHERE clauses in LINQ to SQL
- LINQ for CRM how to use C# list in where clause
- Using LINQ how do I create a List of one particular field of an entity from a collection entities
- Using LINQ to create pairs from two different list where the entries have same attribute
- How to use Linq to select and group complex child object from a parents list
- When using Linq's GROUP BY to create a list of groups, how can I select a specific group by its key?
- How to query by where clause with EF code first
- C# - Linq optimize code with List and Where clause
- Linq - How to apply a where clause on the maximum element of a list
- How can I get data from a list with a where clause to another list?
- How to Create a Run-Time Computed (NotMapped) Value in Entity Framework Code First
- How to check multiple attributes having unique combination within List where attribute names are available as separate collection. [C#]
- How to create LINQ from complex SQL?
- How to create regex for mathing linq statement NOT contains where clause?
- How to create a predicate that must meet two values with one being a list of values?
- How can i copy data table records of different field name based on mapping list evaluating condition on source data table?
More Query from same tag
- Why does DbFunctions not work with Linq to Entities
- Trying to Add a value as CSV in Linq lambda statement with groupby
- How to sort by a specific key and then get the first result in the sorted collection into a group in LINQ
- Add range to a new list of object within the same command / line
- Intersection of multiple lists with IEnumerable.Intersect()
- XElement LINQ with WHERE condition fails
- Can I update multiple columns using LINQ?
- DataTable select help
- dynamic queries in dapper
- Repository vs Data Access
- How to group by items that can grow?
- Linq Convert.ToInt32 in Query
- Updating LINQ to SQL but the query i have is from a join? Can't update using query that uses a join?
- Selecting elements from XML file using LINQ
- Casting Anonymous Type LINQ Using TryCast Returns Nothing
- Load a DataGridView from LINQ to SQL query
- How to get Json data through LINQ in WCF?
- How to read same child element types from an XML tree recursively?
- Get List<> element position in c# using LINQ
- Optimize EntityFramework Query
- VB.NET LINQ Join 2 tables , then group by a column then sum
- The cast to value type 'Int32' failed because the materialized value is null. Pls help me how to fixed it
- Linq to group by firstname lastname and select from a sub array
- Force a main Thread element to run in another created thread
- LINQ Normalizing data
- LINQ join query with multiple fields using left outer join
- LINQ select non-empty strings
- Error in query:LINQ to Entities does not recognize the method 'System.String ToString()' method
- Linq Query using navigation properties and Where clause
- Group by and Join two tables via Linq and nested foreach