Mine W
Mine W

Reputation: 55

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?

Upvotes: 4

Views: 182

Answers (2)

Moriya
Moriya

Reputation: 7906

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

how to do subquery in LINQ

Upvotes: 1

Nuffin
Nuffin

Reputation: 3972

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)

Upvotes: 5

Related Questions