Skip to main content
 首页 » 编程设计

c#之您如何将此查询转换为方法语法

2024年02月13日11tintown

我现在很好奇如何转换此查询语法:

Esperienze = (from e in Esperienze 
             from p in e.PeriodiValidita.Cast<PeriodoValidita>() 
             where UiUtils.IntersectDatesRanges(DateTime.Parse(p.ValidoDal), DateTime.Parse(p.ValidoAl), DataInizioDT, DataFineDT) 
             select e).Distinct(); 

LINQ 中的方法语法。 .SelectMany?

请您参考如下方法:

根据 this我明白了

Esperienze 
    .SelectMany(e => e.PeriodiValidita.Cast<PeriodoValidita>(), (e, p) => new { e, p }) 
    .Where(τ0 => UiUtils.IntersectDatesRanges( 
        DateTime.Parse(τ0.p.ValidoDal), 
        DateTime.Parse(τ0.p.ValidoAl), 
        DataInizioDT, 
        DataFineDT)) 
    .Select(τ0 => τ0.e) 
    .Distinct() 

对于查询加上Distinct。

在规范中,您要查找 7.16.2.4 7.16.2.4 部分的这一部分 From、let、where、join 和 orderby 子句:

A query expression with a second from clause followed by something other than a select clause:

from x1 in e1 
from x2 in e2 
… 

is translated into

from * in ( e1 ) . SelectMany( x1 => e2 , ( x1 , x2 ) => new { x1 , x2 } ) 
…