将linq结果按值分组,并将空值或无效值按空字符串分组 [英] Group linq results by value and group null or invalid values by empty string

查看:134
本文介绍了将linq结果按值分组,并将空值或无效值按空字符串分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按部分邮政编码分组,如果邮政编码为空或少于3个字符,则将其分组为"

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

我已经看到了使用可为空的比较器的一些示例,但是不确定如何在下面的上下文中将类似的内容放入语法中.

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.

QBModel.ResultsTable也是一个动态列表,而CallerZipCode是一个char(10),因此具有有效值的值可以是"96701 -----"

Also the QBModel.ResultsTable is a dynamic list and the CallerZipCode is a char(10) so something with the valid value, could be "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();

这是我发现的可为空的比较器,但是如果我要检查少于2个字符的邮政编码,则可能需要工作:

Here is a nullable comparer I found but probably needs work if I'm going to check for zip codes less than 2 characters:

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?

刚刚尝试过类似的操作,但是效果似乎不太好

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:

您要按邮政编码分组,但是如果邮政编码为空或空或少于3个字符,则要将它们放入组"<null>".

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>".

如果这是您想要的,则可以尝试以下操作:

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();

对于GetGroupRepresentation具有以下实现:

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

    return zipCode;
}

我不明白您为什么要使用Substring方法或StartsWith方法,所以我只是删除了它.

I didn't understand why you are using the Substring-method or StartsWith-method for, so I just removed it.

这是一个完整的例子:

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

这篇关于将linq结果按值分组,并将空值或无效值按空字符串分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆