LINQ获取记录数

时间:2016-01-11 14:05:41

标签: c# sql-server entity-framework linq

我需要将此SQL查询重写为LINQ EF:

SELECT Filter,Attribute,AttributeGroup,AttributeGroupTop,AttributeType,AttributeName,AttributeColor,AttributeSequence,AttributeImage,Sequence,
CASE AttributeType 
WHEN '1' THEN
(Select COUNT(DISTINCT ItemID) FROM NavFilters with(nolock) WHERE Link='lighting' AND SubLink='chandeliers' AND Attribute1=Web_AttributesSchemaEx.Attribute)
WHEN '2' THEN
(Select COUNT(DISTINCT ItemID) FROM NavFilters with(nolock) WHERE Link='lighting' AND SubLink='chandeliers' AND  Attribute2=Web_AttributesSchemaEx.Attribute)
WHEN '3' THEN
(Select COUNT(DISTINCT ItemID) FROM NavFilters with(nolock) WHERE Link='lighting' AND SubLink='chandeliers' AND  Attribute3=Web_AttributesSchemaEx.Attribute)         
WHEN '4' THEN
(Select COUNT(DISTINCT ItemID) FROM NavFilters with(nolock) WHERE Link='lighting' AND SubLink='chandeliers' AND  Attribute4=Web_AttributesSchemaEx.Attribute)         
WHEN '5' THEN
(Select COUNT(DISTINCT ItemID) FROM NavFilters with(nolock) WHERE Link='lighting' AND SubLink='chandeliers' AND  Attribute5=Web_AttributesSchemaEx.Attribute)         
WHEN '6' THEN
(Select COUNT(DISTINCT ItemID) FROM NavFilters with(nolock) WHERE Link='lighting' AND SubLink='chandeliers' AND  Attribute6=Web_AttributesSchemaEx.Attribute)         
WHEN '7' THEN
(Select COUNT(DISTINCT ItemID) FROM NavFilters with(nolock) WHERE Link='lighting' AND SubLink='chandeliers' AND  Attribute7=Web_AttributesSchemaEx.Attribute)         
WHEN '8' THEN
(Select COUNT(DISTINCT ItemID) FROM NavFilters with(nolock) WHERE Link='lighting' AND SubLink='chandeliers' AND  Attribute8=Web_AttributesSchemaEx.Attribute)         
WHEN 'P' THEN
(Select COUNT(DISTINCT ItemID) FROM NavFilters with(nolock) WHERE Link='lighting' AND SubLink='chandeliers' AND  AttributeP=Web_AttributesSchemaEx.Attribute)         
END
AS Count

FROM Web_AttributesSchemaEx with(nolock) WHERE (AttributeType='P' AND Website='LXD') OR (Link='lighting' AND SubLink='chandeliers'  AND Website='LXD')
ORDER BY AttributeType,AttributeSequence

这是我的代码:

db.Web_AttributesSchemaEx.Select(e => new
                {                    
                    Link = e.Link,
                    SubLink = e.SubLink,
                    Website = e.Website,
                    Filter = e.Filter,
                    AttributeType = e.AttributeType,
                    AttributeName = e.AttributeName,
                    AttributeSequence = e.AttributeSequence,
                    Count = e.AttributeType == "1" ? db.NavFilters.GroupBy(x => x.ItemID).Select(x => x.FirstOrDefault()).Count(x => x.Attribute1 == e.Attribute && x.Link == link && x.SubLink == subLink) :
                            e.AttributeType == "2" ? db.NavFilters.GroupBy(x => x.ItemID).Select(x => x.FirstOrDefault()).Count(x => x.Attribute1 == e.Attribute && x.Link == link && x.SubLink == subLink) :
                            e.AttributeType == "3" ? db.NavFilters.GroupBy(x => x.ItemID).Select(x => x.FirstOrDefault()).Count(x => x.Attribute1 == e.Attribute && x.Link == link && x.SubLink == subLink) :
                            e.AttributeType == "4" ? db.NavFilters.GroupBy(x => x.ItemID).Select(x => x.FirstOrDefault()).Count(x => x.Attribute1 == e.Attribute && x.Link == link && x.SubLink == subLink) :
                            e.AttributeType == "5" ? db.NavFilters.GroupBy(x => x.ItemID).Select(x => x.FirstOrDefault()).Count(x => x.Attribute1 == e.Attribute && x.Link == link && x.SubLink == subLink) :
                            e.AttributeType == "6" ? db.NavFilters.GroupBy(x => x.ItemID).Select(x => x.FirstOrDefault()).Count(x => x.Attribute1 == e.Attribute && x.Link == link && x.SubLink == subLink) :
                            e.AttributeType == "7" ? db.NavFilters.GroupBy(x => x.ItemID).Select(x => x.FirstOrDefault()).Count(x => x.Attribute1 == e.Attribute && x.Link == link && x.SubLink == subLink) :
                            e.AttributeType == "8" ? db.NavFilters.GroupBy(x => x.ItemID).Select(x => x.FirstOrDefault()).Count(x => x.Attribute1 == e.Attribute && x.Link == link && x.SubLink == subLink) :
                            e.AttributeType == "P" ? db.NavFilters.GroupBy(x => x.ItemID).Select(x => x.FirstOrDefault()).Count(x => x.Attribute1 == e.Attribute && x.Link == link && x.SubLink == subLink) : 0
                }).Where(x => (x.AttributeType == "P" && x.Website == "LXD") || (x.Link == "lighting" && x.SubLink == "chandeliers" && x.Website == "LXD")).OrderBy(x => x.AttributeType).ThenBy(x => x.AttributeSequence);

我的count条记录存在问题。 SQL SP的正确结果: http://prntscr.com/9oqsoz 这是我得到的结果: http://prntscr.com/9oque7 记录顺序是正确的,其他字段也是正确的。但我想知道我错过了什么?有谁能够帮我?这很奇怪,但我注意到这个问题只适用于LinkSubLink

2 个答案:

答案 0 :(得分:1)

我认为SQL CASE表达式的等效LINQ表达式应该是这样的(注意SQL的...COUNT(DISTINCT(ItemID)))..部分)

//...
Count = 
    e.AttributeType == "1" ? db.NavFilters.Where(x => x.Attribute1 == e.Attribute && 
        x.Link == link && x.SubLink == subLink).Select(x => x.ItemID).Distinct().Count() :
    e.AttributeType == "2" ? ...

答案 1 :(得分:0)

尝试这个...而不是您的初始查询 -

SELECT
    Filter,
    Attribute,
    AttributeGroup,
    AttributeGroupTop,
    AttributeType,
    AttributeName,
    AttributeColor,
    AttributeSequence,
    AttributeImage,
    [Sequence],
    [Count] = (
        SELECT COUNT(DISTINCT
            CASE WHEN
                    (e.AttributeType = '1' AND n.Attribute1 = e.Attribute) OR
                    (e.AttributeType = '2' AND n.Attribute2 = e.Attribute) OR
                    (e.AttributeType = '3' AND n.Attribute3 = e.Attribute) OR
                    (e.AttributeType = '4' AND n.Attribute4 = e.Attribute) OR
                    (e.AttributeType = '5' AND n.Attribute5 = e.Attribute) OR
                    (e.AttributeType = '6' AND n.Attribute6 = e.Attribute) OR
                    (e.AttributeType = '7' AND n.Attribute7 = e.Attribute) OR
                    (e.AttributeType = '8' AND n.Attribute8 = e.Attribute) OR
                    (e.AttributeType = 'P' AND n.AttributeP = e.Attribute)
                THEN n.ItemID END
        )
        FROM NavFilters n
        WHERE n.Link = 'lighting'
            AND n.SubLink = 'chandeliers'
    )
FROM Web_AttributesSchemaEx e
WHERE (AttributeType = 'P' AND Website = 'LXD') 
    OR (Link = 'lighting' AND SubLink = 'chandeliers' AND Website = 'LXD')
ORDER BY AttributeType, AttributeSequence