views:

37

answers:

3
+1  Q: 

LINQ subquery IN

Hi !

I'm a newbie with the IQueryable, lambda expressions, and LINQ in general. I would like to put a subquery in a where clause like this :

Sample code :

SELECT * FROM CLIENT c WHERE c.ETAT IN (
 SELECT DDV_COLUMN_VAL FROM DATA_DICT_VAL
 WHERE TBI_TABLE_NAME = 'CLIENT' AND DD_COLUMN_NAME = 'STATUS'
           AND DDV_COLUMN_VAL_LANG_DSC_1 LIKE ('ac%'))

How do I translate this in LINQ ?

A: 

If you are new to Linq, you absolutely need two essential tools. The first is a tool that converts most T-SQL statements to Linq called Linqer (http://www.sqltolinq.com/). This should take care of the query in your question. The other tool is LinqPad (http://www.linqpad.net/). This will help you learn Linq as you practice with queries.

I often use Linqer to convert a T-SQL query for me, and then use LinqPad to fine tune it.

Randy Minder
Thanks ! I Already have LinqPad, now I'll get the Linqer.
ultraman69
+1  A: 
from c in db.Client
where (from d in db.DataDictVal 
       where d.TblTableName == "Client" 
         && d.DDColumnName == "Status"
         && dd.DdvColumnValLandDsc1.StartsWith("ac"))
       .Contains(c.Etat)
select c;
James Curran
:( You type too quick!
mjmcloug
FYI: You're missing the select expression on the inner query.
kbrimington
+2  A: 
var innerquery = from x in context.DataDictVal
                 where x.TbiTableName == myTableNameVariable
                    && x.DdColumnName == "Status"
                    && x.DdbColumnValLangDsc1.StartsWith("ac")
                 select x.DdvColumnVal;

var query = from c in context.Client
            where innerquery.Contains(c.Etat)
            select c;
kbrimington
We have a winner ! Thanks a lot kbrimington. Altough I would have like to understand a bit more. Would you have some good reading suggestions about LINQ, IQueryable and lambda expressions ? Thanks again !
ultraman69
Although it wouldn't help with this exact scenario, I got a lot of mileage out of reading these examples: http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx. I also learned a lot from Scott Gu's series on Linq-to-SQL: http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx
kbrimington