C# SQL HAVING子句的LINQ查询

C# SQL HAVING子句的LINQ查询,c#,asp.net-mvc,linq-to-sql,C#,Asp.net Mvc,Linq To Sql,[重写:31/01] 我有一个问题,我正试图重新生成一个SQL查询,我曾经有。。。和 下表显示了存储表(ID)和资产(A-F是资产的类型或名称)的简化视图。 我的视图向控制器发布一个名称列表(来自使用MvcCheckBoxList实现的复选框) 我的查询结果应该是一个列表ID,其中每个ID至少选择了一个资产名称 例子 -名称列表仅包括A和B返回ID{4} -名称列表=A、C和E返回id{4和6} SQL查询 上一篇文章中,我使用循环创建了一个文本字符串,该字符串将后续AND子句连接在一起,并作

[重写:31/01] 我有一个问题,我正试图重新生成一个SQL查询,我曾经有。。。和

下表显示了存储表(ID)和资产(A-F是资产的类型或名称)的简化视图。 我的视图向控制器发布一个名称列表(来自使用MvcCheckBoxList实现的复选框)

我的查询结果应该是一个列表ID,其中每个ID至少选择了一个资产名称

例子 -名称列表仅包括A和B返回ID{4} -名称列表=A、C和E返回id{4和6}

SQL查询 上一篇文章中,我使用循环创建了一个文本字符串,该字符串将后续AND子句连接在一起,并作为SQL connectionString传递

上面的示例1如下所示:

SELECT * FROM Stores S, ....(all other columns required)
LEFT JOIN Assets A ON S.ID == A.SID
WHERE (DATE ....)
HAVING (count(case when A.Name == "A" then 1 else NULL end) > 0) AND (count(case when A.Name == "B" then 1 else NULL end) > 0)
GROUP BY S.ID;
这是因为。。我需要复制

Linq查询 我试过这个

stores = (from s in stores 
join a in db.Assets on s.ID equals a.StoreID
where (postedTillNames.Contains(a.Name)).Distinct().ToList();
但这会返回任何具有任何名称的存储。postedTillNames是从模型中的视图发回的,是字符串数组

使用for循环的解决方案

因此,我找到了一个非Linq的解决方案,我将暂时使用它,但如果可能的话,我希望使用干净的Linq(并学习一些东西;-):

List tillStores=new List();
foreach(存储中的var s)
{
布尔r=真;
对于(int c=0;ca.Name==postedTillNames[c]).Count()>0)?true:false;
如果(!r)断裂;
}
若(r)项。添加(s);
}
再次感谢您到目前为止的帖子和评论

问候 克雷格



更新:我已经完全重写了问题,因此请使用以下课堂结构从顶部阅读:

class Store
{
    public int ID { get; set; }
    public string Name { get; set; }
}
class Asset
{
    public int StoreID { get; set; }
    public string Name { get; set; }        
}
和数据:

List<Store> Stores = new List<Store>()
{
   new Store { ID = 1, Name = "First"},
   new Store { ID = 2, Name = "Second"},
   new Store { ID = 3, Name = "Third"},
   new Store { ID = 4, Name = "Fourth"},
   new Store { ID = 5, Name = "Fifth"},
   new Store { ID = 6, Name = "Sixth"}
};

List<Asset> Assets = new List<Asset>()
{
    new Asset { StoreID = 1, Name = "B"},
    new Asset { StoreID = 1, Name = "B"},
    new Asset { StoreID = 1, Name = "F"},
    new Asset { StoreID = 1, Name = "F"},
    new Asset { StoreID = 2, Name = "A"},
    new Asset { StoreID = 2, Name = "A"},
    new Asset { StoreID = 2, Name = "C"},
    new Asset { StoreID = 2, Name = "D"},
    new Asset { StoreID = 3, Name = "B"},
    new Asset { StoreID = 3, Name = "B"},
    new Asset { StoreID = 3, Name = "B"},
    new Asset { StoreID = 4, Name = "A"},
    new Asset { StoreID = 4, Name = "B"},
    new Asset { StoreID = 4, Name = "C"},
    new Asset { StoreID = 4, Name = "C"},
    new Asset { StoreID = 4, Name = "D"},
    new Asset { StoreID = 4, Name = "D"},
    new Asset { StoreID = 4, Name = "D"},
    new Asset { StoreID = 4, Name = "E"},
    new Asset { StoreID = 4, Name = "F"},
    new Asset { StoreID = 5, Name = "D"},
    new Asset { StoreID = 6, Name = "A"},
    new Asset { StoreID = 6, Name = "A"},
    new Asset { StoreID = 6, Name = "C"},
    new Asset { StoreID = 6, Name = "E"}
  };

上述查询返回一个
列表
对象,该对象包含ID=4,6的存储。

使用以下类结构:

class Store
{
    public int ID { get; set; }
    public string Name { get; set; }
}
class Asset
{
    public int StoreID { get; set; }
    public string Name { get; set; }        
}
和数据:

List<Store> Stores = new List<Store>()
{
   new Store { ID = 1, Name = "First"},
   new Store { ID = 2, Name = "Second"},
   new Store { ID = 3, Name = "Third"},
   new Store { ID = 4, Name = "Fourth"},
   new Store { ID = 5, Name = "Fifth"},
   new Store { ID = 6, Name = "Sixth"}
};

List<Asset> Assets = new List<Asset>()
{
    new Asset { StoreID = 1, Name = "B"},
    new Asset { StoreID = 1, Name = "B"},
    new Asset { StoreID = 1, Name = "F"},
    new Asset { StoreID = 1, Name = "F"},
    new Asset { StoreID = 2, Name = "A"},
    new Asset { StoreID = 2, Name = "A"},
    new Asset { StoreID = 2, Name = "C"},
    new Asset { StoreID = 2, Name = "D"},
    new Asset { StoreID = 3, Name = "B"},
    new Asset { StoreID = 3, Name = "B"},
    new Asset { StoreID = 3, Name = "B"},
    new Asset { StoreID = 4, Name = "A"},
    new Asset { StoreID = 4, Name = "B"},
    new Asset { StoreID = 4, Name = "C"},
    new Asset { StoreID = 4, Name = "C"},
    new Asset { StoreID = 4, Name = "D"},
    new Asset { StoreID = 4, Name = "D"},
    new Asset { StoreID = 4, Name = "D"},
    new Asset { StoreID = 4, Name = "E"},
    new Asset { StoreID = 4, Name = "F"},
    new Asset { StoreID = 5, Name = "D"},
    new Asset { StoreID = 6, Name = "A"},
    new Asset { StoreID = 6, Name = "A"},
    new Asset { StoreID = 6, Name = "C"},
    new Asset { StoreID = 6, Name = "E"}
  };

上面的查询返回一个
列表
对象,该对象包含ID=4,6的存储。

我真的不明白您想做什么。请包括您编写的符合您要求的SQL。你的解释有点混乱。你的代码到底有什么问题?嗨。对不起,没有解释。我试图在S.ID=a.StoreID上复制一个SELECT*FROM Store S JOIN Asset S,其中a.NAME=“a”和a.NAME=“B”那么,该数据从何而来?你还没有解释整个情况,对不起。我不久前在SQL中编写了这个查询,使用Select*From Stores S LEFT JOIN Assets a where S.ID=a.S_ID where。。。。。有count(当A.NAME=“A”)>0和count(当A.NAME=“B”)和。。。等。希望这表明我是如何使用have的,同样的业务逻辑现在也适用于这个问题,尽管我已经简化了计算。欢迎向问题提问,不要发表评论作为问题的更新。我真的不明白你想做什么。请包括您编写的符合您要求的SQL。你的解释有点混乱。你的代码到底有什么问题?嗨。对不起,没有解释。我试图在S.ID=a.StoreID上复制一个SELECT*FROM Store S JOIN Asset S,其中a.NAME=“a”和a.NAME=“B”那么,该数据从何而来?你还没有解释整个情况,对不起。我不久前在SQL中编写了这个查询,使用Select*From Stores S LEFT JOIN Assets a where S.ID=a.S_ID where。。。。。有count(当A.NAME=“A”)>0和count(当A.NAME=“B”)和。。。等。希望这表明我是如何使用have的,同样的业务逻辑现在也适用于这个问题,尽管我已经简化了计算。欢迎向问题提问,不要将评论作为问题的更新。谢谢。如果你能解释一下这个组是如何工作的,以及从句在哪里工作的,那就更棒了!谢谢,非常感谢。如果你能解释一下这个组是如何工作的,以及从句在哪里工作的,那就更棒了!谢谢