2017-03-05 5 views
-1

良い一日を数えます。私は最後の2ヶ月でグループリストにしたいと私はリストを持っているすべての月に彼らのどのように多くの製品カウントし、また、各月にどのグループリストへの最後の2ヶ月間によると、C#での個別の項目

を販売するすべての製品のための明確なカテゴリを数えます。 ProductTransactionという名前のクラスが含まれています。

List<ProductTransaction> productList = new List<ProductTransaction>(); 
class ProductTransaction 
{ 

    string product; 
    DateTime date_sold; 
    string category; 
} 

productList.Add(new ProductTransaction() {product="sword", date_sold=03/05/2017,category="weapons"}); 
productList.Add(new ProductTransaction() {product="sword2", date_sold=03/01/2017,category="weapons"}); 
productList.Add(new ProductTransaction() {product="potion", date_sold=02/05/2017,category="life"}); 
productList.Add(new ProductTransaction() {product="jacket", date_sold=02/03/2017,category="clothing"}); 
productList.Add(new ProductTransaction() {product="jacketofBear", date_sold=02/01/2017,category="clothing"}); 

以下のようなデータを得たいです。あなたは現在の月の始まりを取得するには、現在の日付から現在の日付を減算して、以前の始まりを取得するために1ヶ月減算することにより、あなたが望む最も早い日付を計算することができまず

Month --  productCount  --  categoryCount 

March --    2   --    1 

February --    3   --    2 
+0

あなたは_last 2 months_によって正確に何を意味するのですか?あなたの事例が2ヶ月以上の表現をしていたら、2つしか望みませんでしたが、もし2つの場合は2つのグループに分けたいと思っていますか?同じ年ですが、別の年ですか? – juharr

+0

私はdate_soldサーを使用して、最後の2ヶ月間の取引を取得したいです。たとえば今月は3月です。今年は3月と2月しか貰えません。 –

+0

あなたは「私が欲しい」と言います。それは他人にあなたのコードを書くように頼むように思える。私はあなた自身を試したと確信していますが、あなたがどこにいるのかを知るのに役立ちます。 –

答えて

1

添付画像ファイルを参照してください月。次に、あなたは、あなたのコレクションをフィルタリングするためにそれを使用したい月に、グループや製品とカテゴリの明確な数を選択します。

DateTime beginningOfPreviousMonth = DateTime.Today 
    .AddDays(-DateTime.Today.Day) 
    .AddMonths(-1); 
var results = productList.Where(p => p.date_sold.Date >= beginningOfPreviousMonth) 
    .GroupBy(p => p.date_sold.Month) 
    .Select(grp => new 
    { 
     Month = new DateTime(1,key,1).ToString("MMMM"), 
     ProductCount = grp.Select(x => x.product).Distinct().Count(), 
     CategoryCount = grp.Select(x => x.category).Distinct().Count() 
    }); 

foreach(var result in results) 
    Console.WriteLine(result); 
+0

こんにちは先生、私はあなたのコードを試してみましたが、私は実際に結果をキャプチャしなかったためかもしれないデータ –

+0

@LanceKiddを表示するのに困難を抱えています。私は今それを修正しました。それが問題でない場合は、私に何があるか教えてください。 – juharr

0

次試してみてください。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      ProductTransaction.productList = new List<ProductTransaction>() { 
       new ProductTransaction() {product="sword", date_sold= DateTime.Parse("03/05/2017"),category="weapons"}, 
       new ProductTransaction() {product="sword2", date_sold=DateTime.Parse("03/01/2017"),category="weapons"}, 
       new ProductTransaction() {product="potion", date_sold=DateTime.Parse("02/05/2017"),category="life"}, 
       new ProductTransaction() {product="jacket", date_sold=DateTime.Parse("02/03/2017"),category="clothing"}, 
       new ProductTransaction() {product="jacketofBear", date_sold=DateTime.Parse("02/01/2017"),category="clothing"} 
      }; 

      var results = ProductTransaction.productList 
       .Where(x => (x.date_sold >= DateTime.Parse("1/1/2017")) && (x.date_sold <= DateTime.Parse("3/31/2017"))) 
       .GroupBy(x => x.date_sold.Month) 
       .Select(x => new 
       { 
        month = x.Key, 
        year = x.FirstOrDefault().date_sold.Year, 
        category_count = x.GroupBy(y => y.category).Count(), 
        product_count = x.Count() 
       }).ToList(); 
     } 
    } 

    class ProductTransaction 
    { 
     public static List<ProductTransaction> productList = new List<ProductTransaction>(); 

     public string product; 
     public DateTime date_sold; 
     public string category; 
    } 
} 
+1

このコード断片はOPの質問に答えるかもしれないが、あなたが説明を追加したい場合は、その答えは、より有用であろう。コードのみの回答には品質レビューのフラグが立てられている可能性があります。 –

+0

こんにちは、あなたのコードはカテゴリと製品のカウントのための別個のカウントを得るのにうまく機能しますが、今年の最後の3ヶ月間にどのように制限できますか? –

+0

結果をフィルタリングするためにWHEREを追加しました。更新されたコードをご覧ください。 – jdweng

関連する問題