2012-04-05 8 views
0

さようなら。私はノブです。私は(いくつかの)プログラミング、小さなSQL、LINQ to SQLを知っています。VB.Net:リストからクエリをDymanic LINQクエリにSQL(擬似コード)を変換します。

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

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

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

私は、ユーザーが選択したグループのリストを持つGroupsListというオブジェクトのコレクションを持っています。

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

問題2:BillingGroupのリストを取得したら、各グループを反復処理する必要があります。グループごとに、BillingCodesを繰り返し処理する必要があります。 BillingCodeごとに、BillingCodeのすべての条件を持つWHERE句を生成する必要があります。

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

問題3:ここで私は手掛かりを持っていない部分があります私はそうのようなものを提案します。 LINQ to SQLでクエリを動的に作成する必要があります。グループの数や各グループのコード数は分かりません。

[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:最後に、私は1つのグループにそのクエリをラップしたい

**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 

私はこのようになりますクエリを想像:

は、2つのテーブルがありますby patient_id。新しいものとして選択して終了するもの{キー、トランザクションとして、カウント数}

私は、この知識を無限の読書と検索でまとめました。私は答えを探し続けるつもりですが、どんな助けも大いに感謝しています。

ありがとうございました。

EDIT - ANSWER:

ここでは私のために働いてしまったコードです:私は、最終的な変数にgoliathを使用した理由

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 

は私に聞かないでください。夜遅くにそのコードを作成し、以前の試みはdavidと命名されました。

ありがとうございました!

答えて

0

EDIT:私に恥です。私はVBを使用しませんでしたが、C#。例のIDという名前グラブint型(または列)のリストや配列、およびリスト場合

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

を使用します。しかし、私はいくつかの答えは...

問題1ビットを助ける必要があります願っています本当にあなたが与えるに関する情報との明確な、しかし

SelectMany(x => blabla) 
の使用ではない:Idsは

問題2 ...私はあなたが2つのクエリを作成する必要があると思いますが、データベースから来ていますまた、ここでは実際に明確ではない

はどこか(そうのように言うのは難しいが、再び)

問題3をトリックを行う必要があり、同じグループ内のあなたの興味組合は何ですか?違いがコードだけの場合、なぜ問題1のシステムを使用しないのですか?

「動的問合せ」を構築するには、私はIQueryableをを例

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

問題4のために、古典的なコードのように、「オンデマンド」を構築することができることを言うだけのことができます。私は「ToDictionary()を使用します"拡張子は、KeyValuePairが必要なようですが、もちろん他の方法でもあります。

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()); 
のようなものを読んでいません
+0

あなたの貢献に感謝します!それは問題の私の理解を促進した。私は十分に分かっておらず、読めるC#の本を見つけました。 – ffrugone

関連する問題