1 回答

TA貢獻1797條經驗 獲得超6個贊
我認為我還沒有完全理解你的目的,但這是我的看法:
您希望按郵政編碼進行分組,但如果郵政編碼為 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;
}
- 1 回答
- 0 關注
- 162 瀏覽
添加回答
舉報