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

编程入门 行业动态 更新时间:2024-10-09 07:22:45
本文介绍了将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结果按值分组,并将空值或无效值按空字符串分组

本文发布于:2023-10-30 18:33:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并将   空字符串   linq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!