4

私は現在、このデータのサンプルテーブルを持っています:

ID  | Policy ID     |   History ID  | Policy name
1   |   1           |    0          | Test
2   |   1           |    1          | Test
3   |   2           |    0          | Test1
4   |   2           |    1          | Test1

このうち、ポリシー ID と履歴 ID (MAX) でグループ化したいので、保持したいレコードは ID の 2 と 4 です。

   ID   | Policy ID     |   History ID  | Policy name
    2   |   1           |    1          | Test
    4   |   2           |    1          | Test1

私はLINQでこれをやろうとしましたが、毎回同じ問題に遭遇しました。エンティティをグループ化できますが、ポリシー オブジェクトからプロパティを保持するのではなく、常にプロパティを再定義する必要があるグループになります。そのような:

var policies = _context.Policies.GroupBy(a => a.intPolicyId)
                                            .Select(group => new {
                                                PolicyID = group.Key,
                                                HistoryID = group.Max(a => a.intHistoryID)
                                            });

これは単に「ポリシー ID」と「履歴 ID」を持つオブジェクトのリストを表示するだけです。このオブジェクトには約 50 以上のプロパティがあるため、すべてのプロパティを再定義することなく、Policies オブジェクトから返されるすべてのプロパティが必要です。

私は試した:

        var policies = _context.Policies.GroupBy(a => a.intPolicyId)
                                                    .Select(group => new {
                                                        PolicyID = group.Key,
                                                        HistoryID = group.Max(a => a.intHistoryID)
                                                        PolicyObject = group;
                                                    });

しかし、これはエラーになります。

何か案は?

4

2 に答える 2

3

複合キーでグループ化

_context.Policies.GroupBy(a => new {a.intPolicyId, *other fields*}).Select(
    group=> new {
        PolicyId = group.Key.intPolicyId,
        HistoryId = group.Max(intHistoryId),
        *other fields*
    }
);

別の方法-履歴を取得し、残りのデータと結合するよりも、このようなもの(箱から出しては機能しない、ある程度の改良が必要になる)

var historyIDs = _context.Policies.GroupBy(a=>a.intPolicyId).Select(group => new {
                                            PolicyID = group.Key,
                                            HistoryID = group.Max(a => a.intHistoryID)
                                        });

var finalData = from h in historyIDs
                join p in _context.Policies on h.intPolicyId equals p.intPolicyId
                select new {h.HistoryId, *all other policy fields*}

そしてさらに別の方法では、さらに簡単で、多くの入力を必要としません:):

var historyIDs = _context.Policies.GroupBy(a=>a.intPolicyId).Select(group => new {
                                            PolicyID = group.Key,
                                            HistoryID = group.Max(a => a.intHistoryID)
                                        });

var finalData = from h in historyIDs
                join p in _context.Policies on h.PolicyId equals p.intPolicyId && h.HistoryId equals p.HistoryId
                select p

基本的には、次のSQLクエリとある程度同等です。

select p.*
from Policy p
inner join (
    select pi.policyId, max(pi.historyId)
    from Policy pi
    group by pi.policyId
) pp on pp.policyId = p.policyId and pp.historyId = p.historyId
于 2012-11-15T15:17:53.877 に答える
0

LINQ to Objects では、次のようにします。

var policies = _context.Policies
    .GroupBy(a => a.intPolicyId)
    .Select(g => g.OrderByDescending(p => p.intHistoryID).First());

しかし_context、データベースが関係している可能性があり、これが翻訳されるとは100%確信が持てません。

基本的には、ポリシー ID でグループ化し、各グループ内で履歴 ID で並べ替え、各グループから履歴 ID が最も大きい行を選択します。にあるものとまったく同じ型を返しますPolicies

于 2012-11-15T15:21:14.923 に答える