亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按值對 linq 結果進行分組,并按空字符串對 null 或無效值進行分組

按值對 linq 結果進行分組,并按空字符串對 null 或無效值進行分組

C#
弒天下 2023-07-22 18:16:12
我正在嘗試按部分郵政編碼進行分組,如果郵政編碼為空或少于 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();
查看完整描述

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;

}

http://img1.sycdn.imooc.com//64bbac8a0001b7ee08950289.jpg

查看完整回答
反對 回復 2023-07-22
  • 1 回答
  • 0 關注
  • 162 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號