既然你提到你有一个List<int>
ID,我想你想根据这些 ID 对List<Field>
&
最后根据 ID 对字段进行Records
排序ID
。Field
例如,如果有 2 条记录
record 1 has 3 fields with IDs (1,2,3)
record 2 had 4 fields with IDs (4,3,6,2)
现在,如果List<int>
有值6,2,3
,您希望使用 ID 为 6、2、3 的字段返回记录,并且需要为每条记录对字段进行排序。
因此,对于示例案例,您的结果将是
record 1 has 3 fields with IDs (2,3)
record 2 had 4 fields with IDS (2,3,6)
如果这是您所追求的,那么这里是一个实际示例的代码
List<Field> fields1 = new List<Field>();
fields1.Add(new Field() { Id = 1, Name = "ONE", Value = "one" });
fields1.Add(new Field() { Id = 2, Name = "TWO", Value = "two" });
fields1.Add(new Field() { Id = 3, Name = "THREE", Value = "three" });
List<Field> fields2 = new List<Field>();
fields2.Add(new Field() { Id = 4, Name = "FOUR", Value = "four" });
fields2.Add(new Field() { Id = 3, Name = "THREE", Value = "three" });
fields2.Add(new Field() { Id = 6, Name = "SIX", Value = "six" });
fields2.Add(new Field() { Id = 2, Name = "TWO", Value = "two" });
records.Add(new Record() { Id = 1, Fields = fields1 });
records.Add(new Record() { Id = 2, Fields = fields2 });
//You did mention that you already have this
List<int> SortByFieldIds = new List<int>();
SortByFieldIds.AddRange(new List<int> { 6, 2, 3 });
//before foreach loop records will have 2 records (record1 with fields 1,2,3 and record2 with fields 4,3,6,2)
foreach (Record r in records)
{
r.Fields = r.Fields.Where(f => SortByFieldIds.Contains(f.Id)).OrderBy(f => f.Id).ToList();
}
//after foreach loop records will have 2 records (record1 with fields 2,3 and record2 with fields 2,3,6)