简体   繁体   English

将linq结果按值分组,并将空值或无效值按空字符串分组

[英]Group linq results by value and group null or invalid values by empty string

I am trying to group by a partial zip code and if any are zip codes that are null or less than 3 characters group them as "" 我正在尝试按部分邮政编码分组,如果邮政编码为null或少于3个字符,则将其分组为“”

I've seen some example of using a nullable comparer but not sure of how to fit something like that in the syntax in the context of below. 我已经看到了使用可为空的比较器的一些示例,但是不确定如何在下面的上下文中将类似的内容放入语法中。

Also the QBModel.ResultsTable is a dynamic list and the CallerZipCode is a char(10) so something with the valid value, could be "96701-----" 此外,QBModel.ResultsTable是一个动态列表,而CallerZipCode是一个char(10),因此具有有效值的值可以是“ 96701 -----”

  var newset = (from rst in QBModel.ResultsTable
          group rst by rst.CallerZipCode.Substring(0, 3) into newGroup
          select new DataSourceRecord()
          {
            State = ToTitleCase(newGroup.Select(i => i.CallerState).FirstOrDefault()),
            ZipCode = newGroup.Where(z => z.CallerZipCode.StartsWith(newGroup.Key)).Select(x => x.CallerZipCode.Substring(0, 3)).FirstOrDefault()
          }).ToList();

Here is a nullable comparer I found but probably needs work if I'm going to check for zip codes less than 2 characters: 这是我找到的可为空的比较器,但是如果我要检查邮政编码少于2个字符的话,可能需要工作:

public class NullableComparer<T> : IEqualityComparer<T?> where T : struct
{
    public bool Equals(T? x, T? y)
    {
        if (x == null || y == null)
            return false;
        return x.Equals(y);
    }

    public int GetHashCode(T? obj)
    {
        return obj.GetHashCode();
    }
}

How can I change this code to accomplish what I am after? 如何更改此代码以完成我要完成的工作?

[Edit] [编辑]

Just tried something like this, but it didn't seem to work very well 只是尝试过这样的事情,但效果似乎不太好

  var newset = (from rst in QBModel.ResultsTable
          group rst by rst.CallerZipCode == null || rst.CallerZipCode.Trim().Length() < 3 ? "<null>" : rst.CallerZipCode.Substring(0, 3) into newGroup
          select new DataSourceRecord()
          {
            State = ToTitleCase(newGroup.Select(i => i.CallerState).FirstOrDefault()),
            ZipCode = newGroup.Where(z => z.CallerZipCode.StartsWith(newGroup.Key)).Select(x => x.CallerZipCode.Substring(0, 3)).FirstOrDefault()
          }).ToList();

I don't think that I have completely understood what you are after, but here is my take: 我认为我不完全了解您的需求,但是我的看法是:

You want to group by the zip code, but if the zip code is null or empty or less than 3 characters long, you want to put them in the group "<null>" . 您想按邮政编码分组,但是如果邮政编码为null或空或少于3个字符,则要将其放入"<null>"

If that is what you want, you could try something like the following: 如果这是您想要的,则可以尝试以下操作:

  var newset = (from rst in QBModel.ResultsTable
          group rst by GetGroupRepresentation(rst.CallerZipCode) into newGroup
          select new DataSourceRecord()
          {
            // ...
          }).ToList();

With the following implementation for GetGroupRepresentation : 通过GetGroupRepresentation的以下实现:

private string GetGroupRepresentation(string zipCode)
{
    if (string.IsNullOrEmpty(zipCode) || zipCode.Length < 3)
    {
        return "<null>";
    }

    return zipCode;
}

I didn't understand why you are using the Substring-method or StartsWith-method for, so I just removed it. 我不明白您为什么要使用Substring方法或StartsWith方法,所以我只是删除了它。

Here is a full example: 这是一个完整的示例:

static void Main(string[] args)
{
    var zipcodes = new List<string> { "1234", "4321", null, "", "12" };

    // LINQ Query Syntax
    var groups = from code in zipcodes
                 group code by GetGroupRepresentation(code) into formattedCode
                 select formattedCode;

    // I think this is easier to read in LINQ Method Syntax.
    // var groups = zipcodes.GroupBy(code => GetGroupRepresentation(code));
}

private static string GetGroupRepresentation(string zipCode)
{
    if (string.IsNullOrEmpty(zipCode) || zipCode.Length < 3)
    {
        return "<null>";
    }

    return zipCode;
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM