LINQ subquery IN

Patrice Cote picture Patrice Cote · Aug 13, 2010 · Viewed 20.5k times · Source

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 ?

Answer

kbrimington picture kbrimington · Aug 13, 2010
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;