如何在Linq查询中为多个列忽略大小写

时间:2018-03-15 09:33:02

标签: c# linq

请在下面找到为获取数据而编写的示例查询,我正在寻找对" GroupName"忽略了这个词的情况。

我知道以下选项,并且不想使用这些提供特定输出格式的选项。寻找保留原始案例。

1.TO下() 2.TO Lower()将为大写或小写。

var GroupName = (from p in FilterList
                 where p.GroupName != null
                 group p by new { month = p.ReportedDateTime.Month, year = p.ReportedDateTime.Year, GroupName =p.GroupName  } into d
                 select new
                 {
                     dt = string.Format("{0}/{1}", d.Key.month, d.Key.year),
                         month = d.Key.month,
                         monthName = new DateTime(d.Key.year, d.Key.month, 1).ToString("MMM", CultureInfo.InvariantCulture),
                         count = d.Count(),
                         GroupName = d.Key.GroupName 
                 }).OrderByDescending(g => g.count)
                   .ThenBy(g => g.GroupName).Take(20);

我如何使用" StringComparer.InvariantCultureIgnoreCase"在上面的GroupName列查询中。

1 个答案:

答案 0 :(得分:0)

您可以在分组键上使用ToLower,并从其中一个组成员中检索原始案例。这与所有小组成员的原始案例不同,但这意味着想要将案例忽略并且不将所有小组成员保留在答案中。

var GroupName = (from p in FilterList
                 where p.GroupName != null
                 group p by new { month = p.ReportedDateTime.Month, year = p.ReportedDateTime.Year, GroupName = p.GroupName.ToLower()  } into d
                 select new {
                     dt = string.Format("{0}/{1}", d.Key.month, d.Key.year),
                         month = d.Key.month,
                         monthName = new DateTime(d.Key.year, d.Key.month, 1).ToString("MMM", CultureInfo.InvariantCulture),
                         count = d.Count(),
                         GroupName = d.First().GroupName
                 }).OrderByDescending(g => g.count)
                   .ThenBy(g => g.GroupName).Take(20);