按值对 linq 结果进行分组,按空字符串对空值或无效值进行分组
Group linq results by value and group null or invalid values by empty string
我正在尝试按部分邮政编码分组,如果有任何邮政编码为空或少于 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();
我不认为我已经完全理解你想要什么,但这是我的看法:
您想按邮政编码分组,但如果邮政编码为 null 或为空或长度少于 3 个字符,您希望将它们放入组 "<null>"
。
如果那是你想要的,你可以尝试如下操作:
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-method 或 StartsWith-method,所以我把它删除了。
这是一个完整的例子:
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;
}
我正在尝试按部分邮政编码分组,如果有任何邮政编码为空或少于 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();
我不认为我已经完全理解你想要什么,但这是我的看法:
您想按邮政编码分组,但如果邮政编码为 null 或为空或长度少于 3 个字符,您希望将它们放入组 "<null>"
。
如果那是你想要的,你可以尝试如下操作:
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-method 或 StartsWith-method,所以我把它删除了。
这是一个完整的例子:
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;
}