Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ subquery IN

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 ?

like image 814
Patrice Cote Avatar asked Aug 13 '10 14:08

Patrice Cote


3 Answers

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;
like image 120
kbrimington Avatar answered Oct 08 '22 11:10

kbrimington


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;
like image 27
James Curran Avatar answered Oct 08 '22 10:10

James Curran


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.

like image 40
Randy Minder Avatar answered Oct 08 '22 12:10

Randy Minder