需要帮助来建立数据库查询中的Where()子句

时间:2018-12-09 06:43:31

标签: c# linq

我在为LINQ查询构建.Where()子句时遇到问题。

我需要从数据库中返回RegionsResources参数和cultureName的{​​{1}}列表。如果为IsActive = true,则返回IsActive = false以匹配defaultCultureName

这些是实体(为简洁起见,不包含不相关的属性):

Id

一些数据示例:

public class RegionResources
{
    // FYI: Composite-Key (Id, CultureId)
    public int Id { get; set; }
    public int CultureId { get; set; }
    public bool IsActive { get; set; }
    public string Name { get; set; }
    public virtual Culture Culture { get; set; }
}

public class Culture
{
    public int Id { get; set; }
    public string CultureName { get; set; }
}

这是带有几个无法按需运行的.Where()子句的方法:

1: RegionResources { Id = 1, CultureId = 1, IsActive = true, Name = "Name(en-US)" }
2: RegionResources { Id = 1, CultureId = 2, IsActive = true, Name = "Name(he-IL)" }
3: RegionResources { Id = 2, CultureId = 1, IsActive = true, Name = "Name_2(en-US)" }
4: RegionResources { Id = 2, CultureId = 2, IsActive = FALSE, Name = "Name_2(he-IL)" }
--
1: Culture { Id = 1, CultureName = "en-US" }
2: Culture { Id = 2, CultureName = "he-IL" }

因此要求:

public class GetRegionResources(string cultureName = "he-IL",
                                string defaultCultureName = "en-US")
{
    var query = context.RegionResources

    // This Where() uses '||' and returns ALL that are IsActive=true
    // for both cultureNames
    .Where(r => (r.Culture.CultureName == cultureName && r.IsActive) ||
                r.Culture.CultureName == defaultCultureName)
    // OR
    // This Where() uses '&&' and returns NOTHING (0 records)
    .Where(r => (r.Culture.CultureName == cultureName && r.IsActive) &&
                r.Culture.CultureName == defaultCultureName)
    .ToList();

    return query;
}

应返回以下内容:

GetReionResources("he-IL", "en-US");

注意:应该返回记录(3),而不是返回记录(4),因为即使cultureName = he-IL,IsActive = false(不是true)。

.Where()子句应如何编写?或者,如果不应该使用.Where(),那么正确的查询是什么?

谢谢。

方法的修订:我没有复制初始方法,而是使用名义上的详细信息重新键入了该方法,认为仅需要重写Where()。我还意识到,我将其设置为类而不是方法。这是实际的方法。

2: RegionResources { Id = 1, CultureId = 2, IsActive = true, Name = "Name(he-IL)" }
3: RegionResources { Id = 2, CultureId = 1, IsActive = true, Name = "Name_2(en-US)" }

抱歉,那些抽空投入并尝试根据原始帖子提供帮助的人。谢谢。

2 个答案:

答案 0 :(得分:0)

var query = context.RegionResources
        .Where(r => (r.Culture.CultureName == cultureName && r.IsActive) ||
                         (r.Culture.CultureName == defaultCultureName && r.Name != "Name(en-US)"))
        .ToList();

应该返回

2: RegionResources { Id = 1, CultureId = 2, IsActive = true, Name = "Name(he-IL)" }
3: RegionResources { Id = 2, CultureId = 1, IsActive = true, Name = "Name_2(en-US)" }

答案 1 :(得分:0)

当区域性处于非活动状态时,您真正想要的是设置默认区域性。这可以通过选择来完成。您只想将其设置为非活动状态,因此它需要是该区域性的并集或“活动”和“非活动”项目。

List<Culture> cult = new List<Culture>();
            var c1 = new Culture() { Id = 1, CultureName = "en-US" };
            var c2 = new Culture() { Id = 2, CultureName = "he-IL" };
            cult.Add(c1);
            cult.Add(c2);

            List<RegionResources> rr = new List<RegionResources>();
            rr.Add(new RegionResources() { Id = 1, Culture = c1, IsActive = true, Name = "Name(en-US)" });
            rr.Add(new RegionResources() { Id = 1, Culture = c2, IsActive = true, Name = "Name(he-IL)" });
            rr.Add(new RegionResources() { Id = 2, Culture = c1, IsActive = true, Name = "Name_2(en-US)" });
            rr.Add(new RegionResources() { Id = 2, Culture = c2, IsActive = false, Name = "Name_2(he-IL)" });

            string cultureName = "he-IL";
            var defaultCulture = c1;

            List<RegionResources> result = rr.Where(r => r.Culture.CultureName == cultureName && r.IsActive == true)
                .Union(rr.Where(r => r.Culture.CultureName == cultureName && r.IsActive == false)).Select(x => { x.Culture = defaultCulture; return x; }).ToList();