SQL HAVING CLAUSE的LINQ查询

时间:2015-01-30 21:22:09

标签: c# asp.net-mvc linq-to-sql

[重写:31/01] 我有这个问题,我试图重新生成一个SQL查询,我使用HAVING ... AND。

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

enter image description here

我的查询结果应该是一个列表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;

它是HAVING ..而且我需要重现。

Linq查询 我试过这个

stores = (from s in stores 
join a in db.Assets on s.ID equals a.StoreID
where (postedTillNames.Contains(a.Name)).Distinct().ToList();

但这将返回任何具有任何名称的商店。 postingTillNames是从模型中的View中回发的,是一个字符串数组。

使用for循环的解决方案

所以我发现了一个非Linq的解决方法,我将暂时使用它,但是如果可能的话,我想使用干净的linq(并学习一些东西; - ):

List<Store> tillStores = new List<Store>();
foreach (var s in stores)
{
    bool r = true;

    for (int c = 0; c < postedTillNames.Count(); c++)
    {
        r = (s.Assets.Where(a => a.Name == postedTillNames[c]).Count() > 0) ? true : false;
        if (!r) break;
    }

    if (r) tillStores.Add(s);
}

到目前为止,再次感谢您的帖子和评论。

此致 克雷格


更新:我已经完全重写了这个问题所以请从顶部阅读

1 个答案:

答案 0 :(得分:4)

使用以下类结构:

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"}
  };

作为存储在DB中的真实表和数据的模型,您可以使用以下Linq查询来获得您想要的内容:

string[] postedTillNames = { "A", "C", "E"};

var result = (from s in Stores
             join a in Assets on s.ID equals a.StoreID
             group new { A = a, S = s } by a.StoreID into assetsGroup
             where !postedTillNames.Any(p => !assetsGroup.Select(g => g.A.Name)
                                                         .Contains(p))
             select assetsGroup.First().S).ToList();

上述查询返回一个List<Store>对象,其中包含ID = 4,6的商店。