0

ループを linq 式に変換しようとしています。しかし、私がやっているようには機能しないようです:

var customer = GetCustomerFromDatabase(id);
ICollection<Order> customerOrders = null;
if (customer == null)
{
    LogAndThrowCustomerNotFound(id);
}
else
{
    customerOrders = customer.Orders;
}

customer.YearToDateSales = 0.0;
customer.CurrentSales = 0.0;
DateTime today = DateTime.Now;

if (customerOrders != null)
    foreach (var order in customerOrders)
    {
        if (order.SubmittedDate != null 
            && order.SubmittedDate.Value.Year.CompareTo(today.Year) == 0)
        {
            customer.YearToDateSales += (double)order.OrderTotal;
        }

        if (order.SubmittedDate != null 
            && (order.SubmittedDate.Value.Month.CompareTo(today.Month) == 0 
            && order.SubmittedDate.Value.Year.CompareTo(today.Year) == 0))
        {
            customer.CurrentSales += (double)order.OrderTotal;
        }
    }

だから私は、その年に一致する顧客の注文を取得するためにその式を思いつきました...ボットは機能しません。彼の表現順序は空で、今日は矛盾しています。私は今日DateTimeを作成します= DateTime.Now; 式のパラメーターで、さまざまなエラーが発生します...

IEnumerable<Order> cOrders = customerOrders
   .Where((ICollection<Order> order , today) =>
           order.SubmittedDate.Value.Month == today.Month);
4

2 に答える 2

2

ラムダに渡そうとしない方が簡単todayです。とにかく式に閉じられます。

customer.YearToDateSales = customerOrders
    .Where(x => x.SubmittedDate != null && 
                x.SubmittedDate.Value.Year == today.Year)
    .Sum(x => x.OrderTotal);

customer.CurrentSales = customerOrders
    .Where(x => x.SubmittedDate != null && 
                x.SubmittedDate.Value.Month == today.Month &&
                x.SubmittedDate.Value.Year  == today.Year)
    .Sum(x => x.OrderTotal);
于 2013-05-21T17:49:10.247 に答える
0

エラーなしで何が問題なのかを正確に判断するのは難しいですが、おそらくSubmittedDate元のバージョンで null などをチェックする必要があります:

IEnumerable<Order> cOrders = customerOrders
      .Where((ICollection<Order> order , today) => 
         order.SubmittedDate.HasValue && 
         order.SubmittedDate.Value.Month == today.Month);
于 2013-05-21T17:30:32.990 に答える