我有很多客戶。下面是一些示例資料
Id Name Age PriorityLevel
1 Bill 50 1
2 Adam 40 2
3 Cory 60 3
4 Edgar 20 4
我想交換這個串列中的專案,所以使用了這個代碼
public static List<T> Swap<T>(this List<T> list, int indexA, int indexB)
{
T tmp = list[indexA];
list[indexA] = list[indexB];
list[indexB] = tmp;
return list;
}
呼叫代碼
List<Customer> cusList = _uow.Customers.GetAll();
cusList.Swap(ddlItem1.SelectedValue, ddlItem2.SelectedValue);
_uow.Save();
運行上述代碼時,它會交換串列中的專案,但不會交換PriorityLevel.
如果我要將第二項與第一項交換,上面的代碼會以下列方式進行(注意記錄的 ID,它們如何保持不變但順序發生變化 - 無論如何在除錯模式下)
Id Name Age PriorityLevel
2 Adam 40 2
1 Bill 50 1
但我想擁有它,所以它執行以下操作(注意優先級)
Id Name Age PriorityLevel
1 Bill 50 2
2 Adam 40 1
這怎么可能?
uj5u.com熱心網友回復:
這是交換串列條目的通用代碼。你需要更具體的東西
public static void SwapPriorities(this List<Customer> list, int indexA, int indexB)
{
int tmp = list[indexA].Priority;
list[indexA].Priority = list[indexB].Priority;
list[indexB].Priority = tmp;
return list;
}
或者干脆
public static void SwapPriorities(Customer customerA, Customer customerB)
{
int tmp = CustomerA.Priority;
customerA.Priority = customerB.Priority;
customerB.Priority = tmp;
}
uj5u.com熱心網友回復:
當您使用“交換”時,您正在執行“交換串列中的索引位置”操作。
您要求簡單地交換優先級串列中的值。
簡單地寫:
public void SwapPriorityValues(Customer source, Customer target)
{
var tmpValue = source.PriorityLevel;
source.PriorityLevel = target.PriorityLevel;
target.PriorityLevel = tmpValue;
}
打電話給你的兩個客戶。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/444353.html
標籤:C# asp.net-mvc 实体框架
