将 foreach 和 if 转换为 linq 查询
convert foreach and if to linq queries
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 查询..
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))));
但它给我带来了以下错误 @ l.Key
CS0201 Only assignment, call, increment, decrement, and new object expressions can be used as a statement
`
试试下面的代码。我认为这是正确的 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;
}
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;
}
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 查询..
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))));
但它给我带来了以下错误 @ l.Key
CS0201 Only assignment, call, increment, decrement, and new object expressions can be used as a statement
`
试试下面的代码。我认为这是正确的 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;
}
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;
}