0

わかりました。私は初心者です。私は (ある程度の) プログラミング、少しの SQL、およびわずかな LINQ to SQL を知っています。

GOAL : ネストされた ListViews を、LINQ で生成された匿名型の iQueryable にバインドします。GroupBy を使用してネストされた ListView を 'it' キーワードにバインドできるので、LINQ を使用したいと考えています。

SETUP : 一連の条件のグループがあります。条件の各セットは、BillingCodes テーブルに格納されます。BillingCodes の各グループは、BillingGroups テーブルに格納されます。

ユーザーが選択した各 BillingGroup の ID、名前、および NumCodes を格納するカスタム オブジェクトがあります。

ユーザーが選択したグループのリストを持つ GroupsList と呼ばれるこれらのオブジェクトのコレクションがあります。

問題 1 : GroupsList を繰り返し処理して、すべての ID を取得できます。LINQ to SQL の SQL 'WHERE ID IN(カンマで区切られた ID の文字列)' を変換するにはどうすればよいですか? それが最善の方法ですか?

問題 2 : BillingGroups のリストを取得したら、各グループを反復処理する必要があります。グループごとに、BillingCodes を反復処理する必要があります。BillingCode ごとに、BillingCode のすべての条件を含む WHERE 句を生成する必要があります。私は次のようなものを提案します:

for each BillingGroup in BillingGroups
    for each BillingCode in BillingGroup.BillingCodes
        where1 = "..." 
    next
next

問題 3 : ここは、手がかりがない部分です。LINQ to SQL でクエリを動的に作成する必要があります。グループがいくつあるか、各グループにいくつのコードがあるかはわかりません。

2 つのテーブルがあります。

**transactions**
transaction_id
patient_id
svc_date
code
charge
description

**v_patients**
first_name
last_name
patient_id
date_of_birth
insname
active
provider_name

次のようなクエリを想像します。

[Group1] Select MAX(svc_date), patient_id, transaction_id
From (

Select transaction_id, patient_id, svc_date
From transactions join v_patients on patient_id
[Code1] Where code=”” and description contains “” and insurance contains “” and charge >= PriceFloor and charge <= PriceCeiling

UNION

Select transaction_id, patient_id, svc_date
From transactions join v_patients on patient_id
[Code2]Where code=”” and description contains “” and insurance contains “” and charge >= PriceFloor and charge <= PriceCeiling

)
Group By patient_id

UNION

[Group2] Select MAX(svc_date), patient_id, transaction_id
From (

Select transaction_id, patient_id, svc_date
From transactions join v_patients on patient_id
[Code1]Where code=”” and description contains “” and insurance contains “” and charge >= PriceFloor and charge <= PriceCeiling

UNION

Select transaction_id, patient_id, svc_date
From transactions join v_patients on patient_id
[Code2]Where code=”” and description contains “” and insurance contains “” and charge >= PriceFloor and charge <= PriceCeiling

)
Group By patient_id

問題 4 : 最後に、そのクエリを患者 ID でグループ化するクエリにラップしたいと思います。Select as New With {key, it as transactions, num as count()} で終わるもの

私はこの知識を無限の読書と検索でつなぎ合わせました。引き続き回答を探しますが、どんな助けでも大歓迎です。

ありがとう。

編集 - 答え:

これが私のために働いたコードです:

Dim chosenIDs() As Short = (From p In GroupsList _
                                   Select p.ID).ToArray()

        GroupMatchListView.DataSource = Nothing

        If chosenIDs.Length > 0 Then

            Dim db As New AudioRxInternalDataContext
            Dim vf As New VersaformDataContext

            Dim chosenGroups() As BillingGroup = (db.BillingGroups.Where(Function(m) chosenIDs.Contains(m.ID))).ToArray()

            Dim wholeResults As List(Of transaction) = Nothing

            For Each chosenGroup As BillingGroup In chosenGroups
                Dim groupResults As List(Of transaction) = Nothing
                For Each chosenCode As BillingCode In chosenGroup.BillingCodes

                    Dim codePredicate = PredicateBuilder.True(Of transaction)()
                    codePredicate = codePredicate.And(Function(i) i.code.Equals(chosenCode.Code))
                    If Not chosenCode.Description Is Nothing Then codePredicate = codePredicate.And(Function(i) i.description.ToUpper().Contains(chosenCode.Description.ToUpper()))
                    If Not chosenCode.Insurance Is Nothing Then codePredicate = codePredicate.And(Function(i) i.v_patient.insname.ToUpper().Contains(chosenCode.Insurance.ToUpper()))
                    If Not chosenCode.PriceFloor Is Nothing Then codePredicate = codePredicate.And(Function(i) i.charge >= chosenCode.PriceFloor)
                    If Not chosenCode.PriceCeiling Is Nothing Then codePredicate = codePredicate.And(Function(i) i.charge <= chosenCode.PriceCeiling)

                    If groupResults Is Nothing Then
                        groupResults = vf.transactions.Where(codePredicate).ToList()
                    Else
                        groupResults.AddRange(vf.transactions.Where(codePredicate).ToList())
                    End If
                Next

                groupResults = groupResults.GroupBy(Function(r) r.patient_id).SelectMany(Function(g) g.Where(Function(r) r.svc_date = g.Max(Function(a) a.svc_date))).ToList()

                If wholeResults Is Nothing Then
                    wholeResults = groupResults
                Else
                    wholeResults.AddRange(groupResults)
                End If
            Next

            Dim conditionsPredicate = PredicateBuilder.True(Of transaction)()
            conditionsPredicate = conditionsPredicate.And(Function(i) i.v_patient.active = "Y")
            conditionsPredicate = conditionsPredicate.And(Function(i) i.svc_date >= StartDateBox.Text)
            conditionsPredicate = conditionsPredicate.And(Function(i) i.svc_date <= EndDateBox.Text)
            If Not OfficeDropDownList.SelectedValue = "Both" Then conditionsPredicate = conditionsPredicate.And( _
                Function(i) (If(i.v_patient.provider_name, "").ToUpper().Contains(OfficeDropDownList.SelectedValue.ToUpper())))

            wholeResults = wholeResults.Where(conditionsPredicate.Compile()).ToList()

            Dim goliath = From f In wholeResults _
                          Group f By f.v_patient Into Group _
                          Order By v_patient.last_name, v_patient.first_name, v_patient.date_of_birth _
                          Select New With {.PatientID = v_patient.patient_id, .LastName = v_patient.last_name, .FirstName = v_patient.first_name, _
                             .DOB = v_patient.date_of_birth, .Ins = v_patient.insname, .MatchCount = Group.Count(), .Matches = Group}

            GroupMatchListView.DataSource = goliath

            theMatchesLabel.Text = goliath.Count()
        Else
            theMatchesLabel.Text = "0"
        End If

goliathfinal 変数を使用した理由を聞かないでください。私はそのコードを深夜に作成し、前回の試みはdavid.

みんなの提案に感謝します!

4

1 に答える 1

0

編集:恥ずかしい:私はVBを使用しませんでしたが、C#を使用しました。しかし、いくつかの答えが少し役立つことを願っています...

問題 1 :たとえば、ID という名前の int (または文字列) のリストまたは配列を取得して使用する

.Where(m => Ids.Contains(m.Id)

Id のリストがデータベースから取得された場合、2 つのクエリを作成する必要があると思います...

問題2:あなたが提供する情報ではあまり明確ではありませんが、

SelectMany(x => blabla)

どこかでトリックを行う必要があります(ただし、そのように言うのは難しいです)

問題 3:ここでもあまり明確ではありません: 同じグループ内の組合の利益は何ですか? 違いがコードだけなら、なぜ問題 1 のシステムを使わないのですか?

「動的クエリ」を構築するには、IQueryable を従来のコードのように「オンデマンド」で構築できると言えます。

var query = blabla;
if (searchCriterion.Name != null)
  query = query.Where(m => m.Name == searchCriterion.Name);

問題 4 : KeyValuePair が必要なように見えるので、"ToDictionary()" 拡張機能を使用しますが、もちろん他の方法もあります。

GroupBy(m => m.TransactionId).ToDictionary(m => m.Key, m => m.Count)

しかし...もう少し具体的にできれば、多分;)

編集問題4: よく読まなかった、むしろそのようなもの

GroupBy(m => m.TransactionId).Select(g => new {
patientId = g.Key,
transaction = g.SelectMany(p => p.Transactions),
num = g.Count());
于 2012-04-05T09:52:18.580 に答える