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();

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;
}
}
- 2 回答
- 0 關注
- 125 瀏覽
添加回答
舉報