views:

101

answers:

2

Hi, I am working with a quite tricky SQL-Query that I would like to translate to LINQ.

Do you think it is possible?

WITH ConditionalChecks AS (
    SELECT c.ItemId FROM ConditionalProperties c, Properties p
    WHERE c.PropertyId = p.Id AND c.IsChecked = 1 AND (

     (p.SystemName = 'eatable') OR
     (p.SystemName = 'diy')

    )
),

ConditionalCount AS (
    SELECT ItemId, Count(*) AS NumTrue FROM ConditionalChecks
    GROUP BY ItemId
),

ItemResult AS (
    SELECT * FROM ConditionalCount c, Items i
    WHERE c.ItemId = i.Id
)

SELECT * FROM ItemResult
WHERE NumTrue = 2

Any hints are appreciated!

+1  A: 

I think something like this should work

Select * FROM (
    Select c.ItemID, Count(*) As NumTrue
    FROM ConditionalProperties c, Properties p
    WHERE c.PropertyId = p.Id AND c.IsChecked = 1 AND (
    (p.SystemName = 'eatable') OR
    (p.SystemName = 'diy')
    GROUP BY ItemID
) AS ConditionalCount 
INNER JOIN Items AS I
ON  ConditionalCount.ItemID = I.id
WHERE ConditionalCount.NumTrue = 2

This is SQL, don't know why I thought you were doing LINQ, and wanted SQL, but this is a much simpler form of the SQL that should do the exact same thing as your multiple queries.

Kibbee
I doubt this is really LINQ - or is it? That's what the OP asked for...
marc_s
It doesn't look like SQL I see, so I think my brain just switched it for some reason. Weird.
Kibbee
That works, but it's just another way of writing the exact same SQL.I'm looking for the LINQ translation :)
Yeah, I guess my brain shuts off on the weekend. Anyway, maybe the simpler form of the SQL is easier to convert into LINQ. Sorry, I'm not that familiar with LINQ, so I can't help you there. I'm leaving this up so maybe another poster who's more fluent with LINQ can have an easier time converting the query to LINQ.
Kibbee
+2  A: 

LINQ makes it easy to aggregate queries like this.

var conditionalChecksQuery = 
    from c in db.ConditionalProperties
    where c.IsChecked == 1  // or 'true', if boolean
    join p in db.Properties on c.PropertyId equals p.Id
    where p.SystemName == "eatable" || p.SystemName == "diy"
    select c.ItemId;

var conditionalCountQuery = 
    from c in conditionalChecksQuery
    group c by c.ItemId into cGrouped
    select new { ItemId = cGrouped.Key, NumTrue = cGrouped.Count() };

var itemResultQuery = 
    from c in conditionalCountQuery
    join i in db.Items on c.ItemId equals i.Id
    select new { Item = i, NumTrue = c.NumTrue };

var finalQuery =
    from result in itemResultQuery
    where result.NumTrue == 2
    select result;
Ben M
Thanks Ben, that's exactly was I was looking for. I just learnt ALOT from this, thanks :)
Glad to be of help :-)
Ben M
Ben i have one doubt . If we are using LINQ we want to keep everything in separate obj and do the operation.So it will reduce the execution speed ,When compare with SQL Procedure?
anishmarokey
SQL procedures have the advantage of plan-caching, but otherwise it should not be any slower to do this in LINQ. I'm not sure if you mean that the queries are executed one-by-one, but if so, they are not: only when the final query is enumerated over is the entire query translated to SQL and executed once.
Ben M
Thanks Ben.!!!Your last point is good and new to me!!! really thanks
anishmarokey
No problem. Here is one of many articles on that feature, which is called 'deferred execution': http://blogs.msdn.com/charlie/archive/2007/12/09/deferred-execution.aspx
Ben M