我看起来完全一样
但是“in lambdas”语法。
我真的很需要这个,有人可以帮忙吗?谢谢!
以下是您在查询语法和 lambda 语法中引用的帖子中的代码片段:https ://stackoverflow.com/a/12324116/670028 :
LINQ 查询语法:
var listOfString = new List<string>() { "String1", "String2" };
var customers =
(
from a in session.Query<A>()
from b in a.B
from c in b.C
where a.Status == "Active"
&& listOfStrings.Contains( b.SomeField )
&& listOfStrings.Contains( c.someOtherField )
select a )
.ToList();
LINQ lambda 语法:
var listOfString = new List<string>() { "String1", "String2" };
var customers =
(
session.Query<A>()
.SelectMany( a => a.B, ( a, b ) => new { a, b } )
.SelectMany( @t => b.C, ( @t, c ) => new { @t, c } )
.Where( @t => a.Status == "Active"
&& listOfStrings.Contains( b.SomeField )
&& listOfStrings.Contains( c.someOtherField ) ).
Select
( @t => a ) )
.ToList();
如您所见,它不像 LINQ 查询语法那样“干净”,因为每次使用 .SelectMany() 方法时都必须继续投影到新的匿名类型。它还可能与所有这些变量名称有点混淆,这就是为什么当我需要执行这样的查询时我倾向于使用 LINQ 查询语法而不是 lambdas 的原因。
只是为了好玩,这里是一个使用 .SelectMany() 的示例,通过为匿名类型赋予更有意义的名称,它看起来更好更真实:
public void Get_StarPowerCorporations_PendingShipment_Orders_Shipping_To_NewYork_Or_Chicago()
{
var session = UnitOfWork.Current.GetSession();
var listOfCities = new List<string> { "New York", "Chicago" };
var customerAndOrders = session.Query<Customer>()
.SelectMany( c => c.Orders, ( Customer, Orders ) => new { Customer, Orders } )
.SelectMany( CustomerAndOrders => CustomerAndOrders.Orders.OrderDetails, ( CustomerAndOrders, OrderDetails ) => new { CustomerAndOrders, OrderDetails } )
.Where( x => x.CustomerAndOrders.Customer.CompanyName == "StarPower Corporatation"
&& x.CustomerAndOrders.Orders.Status == "Pending Shipment"
&& listOfCities.Contains( x.OrderDetails.City ) )
.Select( x => x.CustomerAndOrders.Customer )
.Fetch( x => x.Orders )
.ToList();
}
public class Customer
{
public int CustomerId { get; set; }
public string CompanyName { get; set; }
public IList<Address> Addresses { get; set; }
public IList<Order> Orders { get; set; }
}
public class Address
{
public int AddressId { get; set; }
}
public class Order
{
public int OrderId { get; set; }
public string Status { get; set; }
public IList<OrderDetail> OrderDetails { get; set; }
}
public class OrderDetail
{
public int OrderDetailId { get; set; }
public string City { get; set; }
}