0

次のコードをメソッド演算子に変換する方法:

var myOrders = from c in customers
               where c.Field<string>("Region") == "WA"
               from o in orders
               where c.Field<string>("CustomerID") == o.Field<string>("CustomerID")
               && (DateTime)o["OrderDate"] >= cutoffDate
               select new { 
                  CustomerID = c.Field<string>("CustomerID"), 
                  OrderID = o.Field<int>("OrderID") 
               };

- - - - -また - - - - -

 var myOrders = from c in customers
               where c.Region == "WA"
               from o in orders
               where c.CustomerID == o.CustomerID
               && o.OrderDate >= cutoffDate
               select new { 
                  CustomerID = c.CustomerID, 
                  OrderID = o.OrderID 
               };

オブジェクト形式の同じコード

4

3 に答える 3

2

I'd actually rewrite this as a join - probably via intermediate variables:

var washingtonCustomers = customers.Where(c => c.Field<string>("Region") == "WA");
var recentOrders = orders.Where(o => (DateTime)o["OrderDate"] >= cutoffDate);

var query = washingtonCustomers.Join(recentOrders, 
                 c => c.Field<string>("CustomerID"),
                 o => o.Field<string>("CustomerID"),
                 (c, o) => new { 
                     CustomerID = c.Field<string>("CustomerID"), 
                     OrderID = o.Field<int>("OrderID") 
                 });
于 2012-09-19T17:42:43.453 に答える
0

クエリ構文の代わりに機能的な Linq 構文を使用したいだけですか? それは次のようになります。

var myOrders = customers
  .Where(c => c.Region == "WA")
  .SelectMany(c => 
    orders
      .Where(o => (o.CustomerID == c.CustomerID)
        && (o.OrderDate > cutoffDate))
      .Select(o => new {
           CustomerID = c.CustomerID,
           OrderID = o.OrderID
         })
   );
于 2012-09-19T17:40:25.033 に答える
0

このコードで試すことができます-に基づいて IEnumerable<KeyValuePair<string, Int32>

public  IEnumerable<KeyValuePair<string, Int32>> YourQuery(DateTime date, string code)
{

           var result = 
           from c in customers
           where c.Field<string>("Region") == code

           from o in orders
           where c.Field<string>("CustomerID") == o.Field<string>("CustomerID")
               && (DateTime)o["OrderDate"] >= date

           select new 
           { 
              CustomerID = c.Field<string>("CustomerID"), 
              OrderID = o.Field<int>("OrderID") 
           };

           return result;
}
于 2012-09-19T17:40:45.030 に答える