In standard SQL your query would look like this:
SELECT myColumn FROM myTableBut lets put another kink in, shall we. I'm not using LINQ, I'm using Lambda.
WHERE myColumn IN (myVal1, myVal2, myVal3)
It turns out my savior was Contains.
Every generic List ( List
So, when creating a new query via LINQ for SQL in Lambda, you get a IQueryable interface with a Where extension method ...
Oh crap, here is the code, this will take to long to fully explain:
MyDataDataContext cx = new MyDataDataContext(); // from the LINQ to SQL dbmlSo what have we, and what was created. Well, we just used LINQ to SQL to generate a query that will look like this:
IQueryableq = cx.MyTables.AsQueryable(); // creates the query object
ListlistOfData = {1,2,3,4};
q.Where( x => listOfData.Contains(x.MyIntValue));
var result = q2.Select(x => x.MyIntValue);
SELECT [t0].[MyIntValue]There is a @p[number] for every value in the list 'listOfData' above. And to me, the generated SQL is pretty good. That is probably what I would write.
FROM [dbo.MyTable] AS [t0]
WHERE [t0.][MyIntValue] in ( @p1, @p2, @p3, @p4 )
1 comment:
At last, a post that's easy to follow
I love LINQ but i do often find myself stuggling to emulate good ol' Sql
cheers
Post a Comment