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

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

在 C# 中按類型字段重新排列對象列表

在 C# 中按類型字段重新排列對象列表

C#
繁花不似錦 2023-09-09 16:29:21
我有一個傳入的警報列表,我使用 MapFunction 作為:private static BPAlerts MapToAlerts(List<IntakeAlert> intakeAlerts)        {            // Make sure that there are alerts            if (intakeAlerts.IsNullOrEmpty()) return new BPAlerts { AllAlerts = new List<BPAlert>(), OverviewAlerts = new List<BPAlert>() };            // All Alerts            var alerts = new BPAlerts            {                AllAlerts = intakeAlerts.Select(                    alert => new BPAlert                    {                        AlertTypeId = alert.AlertTypeId ?? 8100,                        IsOverview = alert.IsOverviewAlert.GetValueOrDefault(),                        Text = alert.AlertText,                        Title = alert.AlertTitle,                        Type = alert.AlertTypeId == 8106 ? "warning" : "report",                        Severity = alert.AlertSeverity.GetValueOrDefault(),                        Position = alert.Position.GetValueOrDefault()                    }).OrderBy(a => a.Position).ToList()            };            // Alerts displayed on the overview page            alerts.OverviewAlerts =                alerts.AllAlerts                    .ToList()                    .Where(a => a.IsOverview && !string.IsNullOrEmpty(a.Title))                    .Take(3)                    .ToList();            return alerts;        }我想要實現一個任務,其中 MaptoAlerts 函數返回一個警報對象,其中包含根據 BPAlert 類型排序的概述警報。如果存在的話,請按以下順序更清楚:確認停業 - 8106 \n 破產 - 8105 \n 缺乏許可 - 8111 \n 調查 - 8109 \n 政府行為 - 8103 \n 投訴模式 - 8104 \n 客戶評論 - 8112 \n 認證 - 8110 \n 濫用BBB 名稱 - 8101 \n 咨詢 - 8107 \n 廣告審查 - 8102 \n
查看完整描述

2 回答

?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

解決方案 #1 訂購值數組

我只是在某種集合中定義這些 id 的順序,可以是一個數組:


var orderArray = new int[]

{

    8106,   // Confirmed Out of Busine

    8105,   // Bankruptcy

    8111,   // Lack of Licensing

    8109,   // Investigations

    8103,   // Government Actions

    8104,   // Pattern of Complaints

    8112,   // Customer Reviews

    8110,   // Accreditation

    8101,   // Misuse of BBB Name

    8107,   // Advisory

    8102,   // Advertising Review

};

然后迭代數組,同時遞增訂單值。循環檢查訂單數組是否包含我正在嘗試評估的訂單值的實際類型 id:


for (int orderValue = 0; orderValue < orderArray.Length; orderValue++)

{

    if (alertTypeId == orderArray[orderValue])

    {

        return orderValue;

    }

}

如果在數組中找不到,則返回可能的最大值:


return int.MaxValue

整個方法如下所示,它將評估警報類型 id 的順序:


public int GetAlertTypeIdOrder(short alertTypeId)

{

    var orderArray = new int[]

    {

        8106,   // Confirmed Out of Busine

        8105,   // Bankruptcy

        8111,   // Lack of Licensing

        8109,   // Investigations

        8103,   // Government Actions

        8104,   // Pattern of Complaints

        8112,   // Customer Reviews

        8110,   // Accreditation

        8101,   // Misuse of BBB Name

        8107,   // Advisory

        8102,   // Advertising Review

    };


    for (int orderValue = 0; orderValue < orderArray.Length; orderValue++)

    {

        if (alertTypeId == orderArray[orderValue])

        {

            return orderValue;

        }

    }


    return int.MaxValue;

}

用法:


var sortedAlerts = alerts

    .AllAlerts

    .OrderByDescending(a => GetAlertTypeIdOrder(a.AlertTypeId))

    .ToList();

它也以下降的方式工作。


解決方案#2 訂單值字典

您可以通過減少冗余(重復創建存儲順序值的數組)來獲得更好的性能。更好的主意是將順序規則存儲在字典中。我知道下面的代碼也創建了一個數組,但概念是它將被調用一次以獲取然后傳遞的字典。


public Dictionary<int, int> GetOrderRules()

{

    var alertTypeIds = new int[]

    {

        8106,   // Confirmed Out of Busine

        8105,   // Bankruptcy

        8111,   // Lack of Licensing

        8109,   // Investigations

        8103,   // Government Actions

        8104,   // Pattern of Complaints

        8112,   // Customer Reviews

        8110,   // Accreditation

        8101,   // Misuse of BBB Name

        8107,   // Advisory

        8102,   // Advertising Review

    };


    var orderRules = new Dictionary<int, int>();


    for (int orderValue = 0; orderValue < alertTypeIds.Length; orderValue++)

    {

        orderRules.Add(alertTypeIds[orderValue], orderValue);

    }


    return orderRules;

}

因此該GetAlertIdOrder()方法看起來會有所不同,但仍然保留以前解決方案的想法:


public int GetAlertIdOrder(short alertTypeId, IDictionary<int, int> orderRules)

{

    if (orderRules.TryGetValue(alertTypeId, out int orderValue))

    {

        return orderValue;

    }

    else

    {

        return int.MaxValue;

    }

}

用法:


var orderRules = GetOrderRules();

var sortedAlerts = alerts

    .AllAlerts

    .OrderBy(a => GetAlertIdOrder(a.AlertTypeId, orderRules))

    .ToList();


查看完整回答
反對 回復 2023-09-09
?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

(a) 我不會將排序與映射器混合使用。讓映射器做它自己的事情。(這是關注點分離).. 又名,沒有排序/排序。恕我直言,你總是會在映射器中遇到太多難以理解的巫術。通過上面的代碼,您已經走上了這條道路。


(b) 如果“OverviewAlerts”是 AllAlerts 的子集(又名 AllAlerts 是超集),則水合 AllAlerts,并創建一個只讀“get”屬性,您可以在其中按其規則將 AllAlerts 篩選到您的子集。(可選)考慮 AllAlertsSorted get 屬性。這樣,您就可以讓消費者選擇是想要原始的還是排序的……因為排序是有成本的。


public class BPAlerts

    {

        public List<BPAlert> AllAlerts { get; set; }


        public List<BPAlert> OverviewAlerts {

            get

                {

                    return null == this.AllAlerts ? null : this.AllAlerts.Where (do you filtering and maybe sorting here ) ; 

                } 

            }

    }


        public List<BPAlert> AllAlertsSorted{

            get

                {

                    return null == this.AllAlerts ? null : this.AllAlerts.Sort(do you filtering and maybe sorting here ) ; 

                } 

            }

    }

如果您執行只讀屬性,那么您將擁有更簡單的 linq 操作,例如


OrderBy(x => x.PropertyAbc).ThenByDescending(x => x.PropertyDef);

我 99% 的映射代碼都是這樣的。如果您提供空輸入,我什至不會拋出錯誤,我只是返回空。


public static class MyObjectMapper {


    public static ICollection < MyOtherObject > ConvertToMyOtherObject(ICollection <MyObjectMapper> inputItems) {


        ICollection <MyOtherObject> returnItems = null;


        if (null != inputItems) {

            returnItems = new List <MyOtherObject> ();


            foreach(MyObjectMapper inputItem in inputItems) {


                MyOtherObject moo = new MyOtherObject();

                /* map here */

                returnItems.Add(moo);

            }

        }


        return returnItems;

    }

}


查看完整回答
反對 回復 2023-09-09
  • 2 回答
  • 0 關注
  • 125 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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