我有一個串列:zephyrPatientDataList 有一個非常簡化的模型:
public class zephyrPatientDataList
{
public List<zephyrPatientData> patients { get; set; }
}
public class zephyrPatientData
{
public int CustomerID { get; set; }
public int ClaimID { get; set; }
public string ChemistID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<zDispenseScriptData> scripts{get;set;}
}
public class zDispenseScriptData
{
public bool claimItem { get; set; }
public string id { get; set; }
}
這是系結到資料網格的串列。該串列顯示患者,然后顯示患者列出的腳本(處方藥)。
從這些我有每個腳本的復選框(系結到 [claimItem] (bool))
我讓用戶檢查每個患者的腳本。
在此之后,我需要將腳本上傳到我的 API。
我有兩個選擇:
- 上傳所有腳本(可能是 200-300 個)腳本可能超過 10-20 名患者。
要么
- 創建標記為的患者和腳本串列
索賠項 = TRUE
我試圖創建當前串列的副本(patientList => uploadPatientList)
然后我嘗試遍歷 uploadPatientList 并洗掉所有 claimItem = FALSE 的腳本。
foreach (var newPatient in newList.patients)
{
if(newPatient.scripts != null){
for (int i = newPatient.scripts.Count - 1; i >= 0; --i)
{
if (!newPatient.scripts[i].claimItem)
{
newPatient.scripts.Remove(newPatient.scripts[i]);
}
}
}
}
The issue with this (as I found out) is that since they are based on the same memory object, my original list is thus changed. The issue here is that the data shown to the patient now changes to only show checked items.
How do I "clone" a list and then remove items OR create a list but only with the
myList.patients.scripts which have claimItem = true?
uj5u.com熱心網友回復:
向 zephyrPatientData 添加一個串列,該串列只是宣告的腳本
public class zephyrPatientData
{
public int CustomerID { get; set; }
public int ClaimID { get; set; }
public string ChemistID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
private List<zDispenseScriptData> _scripts
public List<zDispenseScriptData> scripts
{
get => _scripts
set
{
_scripts = value;
claimedScripts = value.Where(s => s.claimItem).ToList();
}
}
public List<zDispenseScriptData> claimedScripts { get; private set; }
}
使用 MVVM 方法,您還可以讓視圖模型為您維護宣告的串列,方法是注意 claimItem 何時更改為 true 并將它們自己添加到宣告的腳本串列中。
uj5u.com熱心網友回復:
使用 System.Linq :如果新的串列資料只是為了讀取,沒關系,因為“ToList()”將新建一個串列。
List<zDispenseScriptData> NewList = YourList.Where(p=>p.claimItem == true).ToList();
也許您只是創建一個 struct 而不是 class ,它自然地按值復制,如下所示:
public struct zephyrPatientData { //.... }
uj5u.com熱心網友回復:
如果創建“深拷貝”是您的目標,那么您可以簡單地序列化和反序列化同一個串列(盡管可能存在一些性能問題,具體取決于串列的型別及其大小)
public List<zephyrPatientDataList> Clone(List<zephyrPatientDataList> original)
{
string serializedList = JsonConvert.Serialize(original);
var clonedList = JsonConvert.Deserialize<List<zephyrPatientDataList>>(serializedList);
return clonedList;
}
然后您可以對克隆串列進行操作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/452109.html
上一篇:如何在r中的串列中跨資料框選擇列
