2011-06-29 9 views
2

私はNHibernateはでこのクエリを実行します。私は、リストが欲しいLINQの集計や一覧

var test = _session.CreateCriteria(typeof(Estimation)) 
       .SetFetchMode("EstimationItems", FetchMode.Eager) 
       .List(); 

アン「推定」いくつかの「EstimationItems」を持つことができます(数量、価格とのProductIdを)

  1. 絵の「推定」のコード(例:2011/0001および2011/0003)ずつ行:推定によって
  2. は(上の意味これらの制約と「推定」の各ライン)私は、構造が以下の画像で明確になることを願っそれぞれ「EstimationItems」

ための各ライン上の推定(手段)合計金額(数量*価格)による「EstimationItems」

  • の数。

    おかげで、

    enter image description here

    enter image description here

  • 答えて

    0
    Dictionary<string, Tuple<int,decimal>> dico = new Dictionary<string, Tuple<int,decimal>>(); 
    foreach (var itemEstimation in test) 
    { 
        Estimation estimation = (Estimation)itemEstimation; 
        if (dico.ContainsKey(estimation.Code) == false) 
        { 
         decimal total = 0; 
         foreach (var item in estimation.EstimationItems) 
         { 
          EstimationItem estimationItem = (EstimationItem)item; 
          total += item.Price * item.Quantity; 
         } 
         dico.Add(estimation.Code, new Tuple<int, decimal>(estimation.EstimationItems.Sum(x => x.Quantity), total)); 
        } 
    } 
    
    List<TestingUI> finalResult = new List<TestingUI>(); 
    foreach (var item in dico) 
    { 
        Tuple<int, decimal> result; 
        dico.TryGetValue(item.Key, out result); 
        finalResult.Add(new TestingUI() { Code = item.Key, Quantity = result.Item1, Total = result.Item2 }); 
    } 
    
    +0

    "に置き換えることができますか?あなたはコードの最初の出現を考慮に入れているようです。また、TestingUI.Quantityには、前記コードまたはすべての見積もりに含まれるアイテムの総数が前記コードで記載されていると想定されますか? –

    2

    は、ここでの命題です:

    var stats = 
        from estimation in test 
        group estimation by estimation.code into gestimation 
        let allItems = gestimation.SelectMany(x => x.EstimationItems) 
        select new 
         { 
          Code = gestimation.Key, 
          ItemNumber = allItems.Count(), 
          TotalPrice = allItems.Sum(item => item.Price * item.Quantity) 
         }; 
    

    さて、これは、あなたが推定(コードを望んでいた3つのプロパティを持つ匿名型を作成し、数この推定コードのアイテムの合計価格、およびこの推定コードのアイテムの合計価格)。

    特定のニーズに合わせて調整できます。 allItemsは、同じコードの見積もりに属するすべてのEstimationItemを含むIEnumerable<EtimationItem>です。

    このオブジェクトを作成するメソッドの範囲外で使用する場合(匿名型ではできない)、これらの値を保持するクラスを作成する必要があります。

    修正さ命題: 命題:

    var stats = 
        (from est in test.Cast<Estimation>() 
        group est by est.code into gEst 
        let allItems = gEst.SelectMany(est => est.EstimationItems).Cast<EstimationItem>() 
        select new TestingUI 
         { 
          Code = gEst.Key, 
          Quantity = gEst.Count(), 
          Total = gEst.Sum(item => item.Price * item.Quantity) 
         }).ToList(); 
    
    +0

    テストは暗黙的であり、私はこのエラーが表示されます。 は、「ソースタイプのクエリパターンの実装を見つけることができませんでした 『System.Collections.IList』。 'GroupBy'が見つかりません。範囲変数 'estimate'の型を明示的に指定することを検討してください。 " –

    +0

    ここでは動作するnhibernateインスタンスはありませんが、リストには使用できないgeneric List () ()?この声はうまくいくが、そうでない場合は、試しに試してみよう.CAST () '。 –

    +0

    私に答えを見て、それは完璧ではないが、それは働いている。改善は歓迎します –