[英]scroll the list until you find the object with the corresponding id - C#
我有一個這樣的列表(已經在visual studio調試器中打開):
如您所見,該列表是由相同類型列表的其他對象組成的,我需要遍歷所有子代,而不管索引如何,並驗證該對象的FID與傳遞的對象相同用戶界面。 找到匹配的服務器后,它將返回相同的對象。
我可以嘗試進行測試,但是考慮到我只有頂級項目(索引為0的那些項目)並沒有全部流通:
AttachmentFolders childWithId17 = ApplicationContext.Instance.companyList[0].AttachmentFolders.SelectMany(parent => parent.AttachmentFolder)
.FirstOrDefault(child => child.FID == "835A09A2-9D60-46CC-A2BE-D4CBC4C81860");
另一張圖片以更好地理解
實際上,我得到了包含許多元素的列表,並且應該全部滾動它,即使以響應方式也可以返回與那個AttachmentFolders FID相對應的對象。
類結構:
public class AttachmentFolders
{
public int id { get; set; }
public String FID { get; set; }
public String Name { get; set; }
public String CPID { get; set; }
public String ParentFID { get; set; }
public List<Attachment> Attachments { get; set; }
public List<AttachmentFolders> AttachmentFolder { get; set; }
}
public class Attachment
{
public int id { get; set; }
public String ATID { get; set; }
public String Name { get; set; }
public String CreatorID { get; set; }
public String FID { get; set; }
public String Extension { get; set; }
public String Description { get; set; }
public int Status { get; set; }
public String CPID { get; set; }
public int FileSize { get; set; }
public DateTime CreationDate { get; set; }
public DateTime ModifiedDate { get; set; }
public int AttachmentType { get; set; }
public int ValidityType { get; set; }
public List<Revisions> Revisions { get; set; }
public String AWID { get; set; }
public String WAID { get; set; }
public String WatermarkPositions { get; set; }
public Boolean Serveroffline { get; set; }
public Boolean IsFavourite { get; set; }
public DateTime LastOpenDate { get; set; }
public int Priority { get; set; }
public String CreatorFirstName { get; set; }
public String CreatorLastName { get; set; }
public String ModifiedByFirstName { get; set; }
public String ModifiedByLastName { get; set; }
public String[] Capabilities { get; set; }
}
謝謝你們。
您可以編寫類似SelectDeep
擴展名的內容,例如此Marc的答案: 在LINQ中表達遞歸
然后在代碼中使用它而不是SelectMany
:
AttachmentFolders child = companyList[0].AttachmentFolders
.SelectDeep(parent => parent.AttachmentFolder)
.FirstOrDefault(child => child.FID == "835A09A2-9D60-46CC-A2BE-D4CBC4C81860");
SelectDeep
方法如下所示:
public static class EnumerableExtensions
{
public static IEnumerable<T> SelectDeep<T>(
this IEnumerable<T> source, Func<T, IEnumerable<T>> selector) {
foreach (T item in source) {
yield return item;
foreach (T subItem in SelectDeep(selector(item), selector)) {
yield return subItem;
}
}
}
}
這是典型的遞歸情況。 你可以嘗試尋找一個扁平的樹形結構,在做這個或這個答案。
如果您遇到此解決方案的性能下降,我會考慮創建一個輔助器Dictionary<string, AttachmentFolders>
對象,該對象將保存所有(子)文件夾引用以進行快速訪問,例如
IEnumerable<AttachmentFolders> Flatten(AttachmentFolders f)
{
var fs = new[] { f };
return f.Children == null? fs : fs.Concat(f.Children.SelectMany(Flatten));
}
Dictionary<string, AttachmentFolders> GenerateCache(AttachmentFolders firstFolder)
{
return Flatten(firstFolder).ToDictionary(f => f.FID, f => f);
}
然后,一次啟動:
Dictionary<string, AttachmentFolders> Cache = GenerateCache(firstFolder);
每次發出請求時:
if(Cache.ContainsKey(fid)) return Cache[fid];
else throw new Exception("Handle not found FID here");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.