Sunday 15 March 2015

sql - How do I write a Linq2Sql query that constrains rows with a collection of composite keys or tuples? -



sql - How do I write a Linq2Sql query that constrains rows with a collection of composite keys or tuples? -

i'd write linq query (sql backend) constrains rows collection of values several columns. illustrate, naive sql query might this, if knew values up-front:

select * mytable (col1 = val1 , col2 = val2 , col2 = val3) or (col1 = val4 , col2 = val5 , col2 = val5) or (col1 = val6 , col2 = val7 , col2 = val8) -- etc...

if writing in sql, consider using bring together against temp table, table valued parameter, or similar construct. how accomplish linq2sql? values not stored in table, computed code , user input, , not known in advance -- receive them collection of tuples.

i know can't bring together against in-memory collections without loading entire table memory first, , in cases limited using contains, resulting in where ... in clause. possible utilize technique composite keys , tuples?

alternatively, manually build look tree where clause emulating where ... or clause in illustration above. there improve way? suspect such sql statement not efficient when there hundreds of or statements in clause.

how this:

var list = mt in context.mytables (mt.col1 == val1 && mt.col2 == val2 && mt.col3 == val3) || (mt.col4 == val4 && mt.col5 == val5 && mt.col6 == val6) // etc etc select mt;

...where val1..val6 etc local variables. pretty direct re-create of 'naïve' sql query.

sql sql-server linq expression-trees composite-key

No comments:

Post a Comment