简体   繁体   中英

SQL Query in C# (Linq)

I've this query in my C# file and it works fine:

from var in db.database_1 
where var.database_2.primarycat.Length > 0 && var.meditype.Contains("All")   
xxx               
select new XElement("id", new XElement("temp", var.database_2.name)

Now, I want to insert this query in the where argument at xxx :

AND name IN (
SELECT primarycat
from database_2
GROUP BY primarycat
HAVING COUNT(*) > 1)

Can somebody help me?

A simple sub query should do this:

from var in db.database_1 
where var.database_2.primarycat.Length > 0
    && var.meditype.Contains("All")   
    && (from cat in db.database_2
        group cat by cat.primarycat into g
        where g.Count() > 1
        select g.Key).Contains(var.name)
select new XElement("id", new XElement("temp", var.database_2.name)

Use a sub select. Check out this thread which answers pretty much the same thing.

how to do subquery in LINQ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM