2016-09-26 79 views
0
foreach (SchemaInfo db in SourceSchemaInfos) 
      { 
       foreach (SchemaInfo table in db.SchemaInfos) 
       { 
        foreach (SchemaInfo tablelist in table.SchemaInfos) 
        { 
         for (int i = 0; i < SelectedTables.Count; i++) 
         { 
          if (tablelist.Key == SelectedTables[i].Key) 
          { 
           foreach (SchemaInfo _tableschema in tablelist.SchemaInfos) 
           { 
            _tableschema.IsSelected = true; 
           } 
          } 
         } 
        } 
       } 
      } 

我試圖上述foreach循環轉換成LINQ查詢,如下..轉換的foreach,如果到LINQ查詢

SourceSchemaInfos.ForEach(Database =>Database.SchemaInfos.ForEach(items => items.SchemaInfos.ForEach(tables => tables.SchemaInfos.Where(tables.Key == SelectedTables.ForEach(l => l.Key)).ForEach(m => m.IsSelected = true)))); 

但thorws我下面的錯誤@l.Key

CS0201 Only assignment, call, increment, decrement, and new object expressions can be used as a statement 

`

回答

2

請嘗試以下代碼。我認爲這是正確的LINQ轉換您的查詢的

foreach (var _tableschema in 
      (from db in SourceSchemaInfos from table in db.SchemaInfos from tablelist in table.SchemaInfos 
      select tablelist) 
      .SelectMany(tablelist => SelectedTables.Where(t => tablelist.Key == t.Key) 
       .SelectMany(t => tablelist.SchemaInfos) 
       )) 
     { 
      _tableschema.IsSelected = true; 
     } 
2
var schemaInfos = from db in SourceSchemaInfos 
        from table in db.SchemaInfos 
        from tableList in table.SchemaInfos 
        from selectedTable in SelectedTables 
        where tableList.Key == selectedTable.Key 
        select tableList.SchemaInfos; 

foreach(var tableSchema in schemaInfos) 
{ 
    tableSchema.IsSelected = true; 
}