简体   繁体   English

对实体框架具有副作用的嵌套 foreach 循环

[英]Nested foreach loops with side effects for Entity Framework

I have problem with nested foreach loops because of following example:由于以下示例,我遇到嵌套 foreach 循环的问题:

var someList = new List<SimpleC>();

simpleA = database.SimpleA.ToList();
simpleB = database.SimpleB.ToList();

foreach (var a in simpleA)
{
    foreach (var b in simpleB)
    {
        var c = new simpleC();
        c.simpleAId = a.Id;
        c.simpleAName = a.Name;
        c.simpleBName = b.Name;

        someList.Add(c);
    }
}

return someList;

The problem: imagine first iteration it goes to first foreach loop then to second foreach loop it initiates new object, maps values and adds newly initiated object to new list, but when second iteration comes instead of going back to first foreach loop it continues with second foreach loop and it loops through second until it finishes, I understand that's how C# works, but I need side effect for it to continue to first foreach loop to get new values.问题:想象第一次迭代它先进入第一个 foreach 循环,然后进入第二个 foreach 循环,它启动新对象,映射值并将新启动的对象添加到新列表,但是当第二次迭代到来而不是回到第一个 foreach 循环时,它继续第二个foreach 循环,它通过第二个循环直到它完成,我知道这就是 C# 的工作方式,但我需要副作用让它继续第一个 foreach 循环以获得新值。

I think, if I'm understanding your question, is that you don't want it to loop through in a nested fashion, but only loop once.我想,如果我理解你的问题,是你不希望它以嵌套的方式循环,而只循环一次。 To do that you'd do it like this.要做到这一点,你会这样做。

for(int i = 0; i < simpleA.Count; i++)
{
    var c = new simpleC();
        c.simpleAId = simpleA[i].Name;
        c.simpleAName = simpleA[i].Name;
        c.simpleBName = simpleB[i].Name;
    someList.Add(c);
}

You would be much better served by not trying to iterate this manually, establishing a relationship and querying it as a join set of entities:如果不尝试手动迭代,建立关系并将其作为实体的连接集进行查询,您会得到更好的服务:

var results = database.SimpleA 
   .Join(database.SimpleB, 
      a => a.ID,       
      b => b.Post_ID,   /
      (a, b) => new { colA = a, colB = b }) 
   .Select new SimpleA(){
      Property1 = a.Col1,
      Property2 = b.Col3,
      Property4 = a.Col2
   }).ToList(); 

This is only mostly functional as a starting point.这主要只是作为一个起点。 You might need a left outer join instead.您可能需要一个左外连接。

Another option, if A and B have a fk constraint in the database, then you may use Navigation properties instead of a join.另一种选择,如果 A 和 B 在数据库中有 fk 约束,那么您可以使用导航属性而不是连接。

If for whatever reason, your loop is required we can try and breakdown the issue:如果出于某种原因需要您的循环,我们可以尝试分解问题:

foreach (var a in simpleA)
{
    foreach (var b in simpleB)  // There is no relationship here!
    {
        var c = new simpleC();
        c.simpleAId = a.Id;
        c.simpleAName = a.Name;
        c.simpleBName = b.Name;

        someList.Add(c);
    }
}

You could try using the index, but that is very likely to cause issues.您可以尝试使用索引,但这很可能会导致问题。

Instead:反而:

foreach (var a in simpleA)
{
    var matchedB = simpleB.Where(x => x.Id == a.Id).FirstOrDefault();

    var c = new simpleC();
    c.simpleAId = a.Id;
    c.simpleAName = a.Name;
    c.simpleBName = matchedB.Name;

    someList.Add(c);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM