按值对 linq 结果进行分组,并按空字符串对 null 或无效值进行分组

我正在尝试按部分邮政编码进行分组,如果邮政编码为空或少于 3 个字符,则将它们分组为“”


我见过一些使用可为空比较器的示例,但不确定如何在下面的上下文中的语法中适应类似的内容。


此外,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();

这是我发现的一个可为 null 的比较器,但如果我要检查少于 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();

    }

}

我怎样才能改变这段代码来完成我所追求的目标?


[编辑]


刚刚尝试过类似的方法,但似乎效果不太好


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


弑天下
浏览 129回答 1
1回答

互换的青春

我认为我还没有完全理解你的目的,但这是我的看法:您希望按邮政编码进行分组,但如果邮政编码为 null 或为空或长度小于 3 个字符,您希望将它们放入组中"<null>"。如果这就是您想要的,您可以尝试以下操作:&nbsp; var newset = (from rst in QBModel.ResultsTable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; group rst by GetGroupRepresentation(rst.CallerZipCode) into newGroup&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select new DataSourceRecord()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).ToList();通过以下实现GetGroupRepresentation:private string GetGroupRepresentation(string zipCode){&nbsp; &nbsp; if (string.IsNullOrEmpty(zipCode) || zipCode.Length < 3)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return "<null>";&nbsp; &nbsp; }&nbsp; &nbsp; return zipCode;}我不明白为什么你要使用 Substring-method 或 StartsWith-method,所以我只是将其删除。这是一个完整的示例:static void Main(string[] args){&nbsp; &nbsp; var zipcodes = new List<string> { "1234", "4321", null, "", "12" };&nbsp; &nbsp; // LINQ Query Syntax&nbsp; &nbsp; var groups = from code in zipcodes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;group code by GetGroupRepresentation(code) into formattedCode&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select formattedCode;&nbsp; &nbsp; // I think this is easier to read in LINQ Method Syntax.&nbsp; &nbsp; // var groups = zipcodes.GroupBy(code => GetGroupRepresentation(code));}private static string GetGroupRepresentation(string zipCode){&nbsp; &nbsp; if (string.IsNullOrEmpty(zipCode) || zipCode.Length < 3)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return "<null>";&nbsp; &nbsp; }&nbsp; &nbsp; return zipCode;}
打开App,查看更多内容
随时随地看视频慕课网APP