Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将foreach中的foreach转换为LINQ,如果它';有可能_Linq_Linq To Sql_Foreach_Linq To Objects_Where Clause - Fatal编程技术网

如何将foreach中的foreach转换为LINQ,如果它';有可能

如何将foreach中的foreach转换为LINQ,如果它';有可能,linq,linq-to-sql,foreach,linq-to-objects,where-clause,Linq,Linq To Sql,Foreach,Linq To Objects,Where Clause,我有一个foreach,它在foreach中,我想把它转换成LINQ。如果它只是一个foreach,我可以使用Where,但我很困惑。这是我的密码 基本上,我需要找到发票,但它取决于元数据,这是2级下降。我认为代码是不言自明的 currentMetaData >>> Is created outside of the foreach code It's an object, but the code below works. foreach (var item in thi

我有一个foreach,它在foreach中,我想把它转换成LINQ。如果它只是一个foreach,我可以使用Where,但我很困惑。这是我的密码

基本上,我需要找到发票,但它取决于元数据,这是2级下降。我认为代码是不言自明的

currentMetaData >>> Is created outside of the foreach code 
It's an object, but the code below works.

foreach (var item in this.invoices.Items)
{
  foreach (var metaData in item.MetaDatas)
  {
    if (metaData == currentMetaData)
    {
      name = item.Name;
      break;
    }
  }
}

我想用LINQ把它缩短。这可能吗?

这将给出所有的名称

 var name = 
             from i in this.invoices
             from m in i.Metadata
             where m == currentMetadata
             select i.Name;
仅第一个值模拟
中断语句

 string name = 
             (from i in this.invoices
             from m in i.Metadata
             where m == currentMetadata
             select i.Name).First();

这将给出所有的名称

 var name = 
             from i in this.invoices
             from m in i.Metadata
             where m == currentMetadata
             select i.Name;
仅第一个值模拟
中断语句

 string name = 
             (from i in this.invoices
             from m in i.Metadata
             where m == currentMetadata
             select i.Name).First();

好东西。。谢谢我想不可能转换为LAMBDAs,因为有两个from??是这样吗?@Martin:不,你可以用lambda表单来处理这个问题,选择多个调用。你为什么在这里调用
ToList
?这不会做任何有用的事情-事实上,它删除了使用First的部分意义(即在到达第一个元素后立即停止计算查询)。@Jon,对,我尝试了不同的形式,在我在妄想中编写不同的形式时,我无知地离开了ToList()。)谢谢你指出,我正在编辑它。嗨,乔恩,你们有一个selectmany用于我的查询的例子吗?这让我感兴趣…很棒的东西。。谢谢我想不可能转换为LAMBDAs,因为有两个from??是这样吗?@Martin:不,你可以用lambda表单来处理这个问题,选择多个调用。你为什么在这里调用
ToList
?这不会做任何有用的事情-事实上,它删除了使用First的部分意义(即在到达第一个元素后立即停止计算查询)。@Jon,对,我尝试了不同的形式,在我在妄想中编写不同的形式时,我无知地离开了ToList()。)谢谢你指出,我正在编辑它。嗨,乔恩,你们有一个selectmany用于我的查询的例子吗?这使我感兴趣。。