score:3

Accepted answer

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

just use like this:

list<(int id1, int id2)> list = new list<(int id1, int id2)>() { (1, 2), (10, 2), (1, 20), (1, 3), (2, 2), (1, 2) /* more data */ };

var result = list.where(x => (x.id1 == 1 && x.id2 == 2) || (x.id1 == 1 && x.id2 == 3));

i've tried it with my own sample data.

enter image description here

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


Related Query

More Query from same tag