0

我有两个列表,一个是列表,Equipment另一个是WorkflowItems带有Equipment属性的列表。列表中的Equipment属性List<WorkflowItem>只有一个水合值,ProcessId. 有List<Equipment>两个特性水合,ProcessIdName。我想List<WorkflowItem>.Equipment.Name用单Equipment条记录中的值来水合List<Equipment>

下面的这个 LINQ 查询将选择一个通用项目,基本上做我正在寻找的,但我宁愿只填写原始列表。

var list = from item in workflowItems
           join equipment in barcodeEquipmentList on 
                             item.Equipment.ProcessId equals equipment.ProcessId
           select new 
           { 
               ProcessId = item.Equipment.ProcessId, 
               EquipmentName = equipment.Name 
           };

编辑 列表将相对较小,即使做这样的事情也可以(除了不起作用的事实)

workflowItems.ForEach(x => x.Equipment = from e in barcodeEquipmentList
                               where e.Process.Id == x.Equipment.Process.Id
                               select e
                           );

...最终编辑

但这确实有效:

workflowItems.ForEach(x => x.Equipment = barcodeEquipmentList
                                           .Where(e => e.Process.Id == x.Equipment.Process.Id)
                                           .FirstOrDefault());
4

2 回答 2

0

Since the dataset was not very big (less than 20 records), I was able to this as below without a hit to performance

workflowItems.ForEach(x => x.Equipment = barcodeEquipmentList
                           .Where(e => e.Process.Id == x.Equipment.Process.Id)
                           .FirstOrDefault());
于 2013-07-16T20:34:36.487 回答
0

这段代码应该符合您的需求:

public class Equipment {
    public int ProcessId { get; set; }
    public string Name { get; set; }
}

public class WorkflowItem {
    public Equipment { get; set; }

    public void LoadEquipmentFrom(IEnumerable<Equipment> cache){
        var equipment = cache.FirstOrDefault(e => e.ProcessId == Equipment.ProcessId);
        if(equipment != null)
            Equipment.Name = equipment.Name;
    }
}

您还可以将缓存中的实例分配给现有的实例,这没关系,因为两者必须具有相同的 Identifier Equipment = equipment;。如果您要设置更多属性,那会更容易。为了进一步优化使用 anIDictionary<int, Equipment>而不是 that IEnumerable<Equipment>,因为您会经常阅读该集合。

我猜你正在实现一种 ORM,在这种情况下,我可以给你一个很好的建议:“已经有一些东西可以满足你的需求。”。

于 2013-07-15T19:21:08.560 回答