LINQ获取记录计数

本文关键字:记录 获取 LINQ | 更新日期: 2023-09-27 18:29:52

我需要将此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的错误。

LINQ获取记录计数

我认为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" ? ...

试试这个。。。而不是您最初的查询-

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