特定の文字列のデータテーブルを検索し、特定の条件(一意のIDフィールド)に基づいてデータテーブル全体のサブセットを返すコードがあります。以下のサンプルコードでは、データテーブル内で "First"と "Employee"を検索し、同じl_id値を持つエントリだけを返します。動的に 'Linqのwhere句を作成する
//Get all Id's that satisfy all conditions:
List<int> results = dtResult.AsEnumerable()
// Get all Id's:
.Select(dataRow => dataRow.Field<int>("l_id"))
// Filter the Id's :
.Where(id =>
// the Id should be greater than one.
id > 1
// and the datatable rows with this Id should have a record with W_Text = First
&& dtResult.AsEnumerable().Any(dataRow => dataRow.Field<string>("W_Text") == "First" && dataRow.Field<int>("l_id") == id)
// and the datatable rows with this Id should have a record with W_Text = Employee
&& dtResult.AsEnumerable().Any(dataRow => dataRow.Field<string>("W_Text") == "Employee" && dataRow.Field<int>("l_id") == id))
.Distinct().ToList();
// Get all datatable rows filtered by the list of Id's.
dtCopy = dtResult.AsEnumerable().Where(dataRow => results.Contains((dataRow.Field<int>("l_id")))).CopyToDataTable();
ここで、検索テキストを動的に取得したらどうすればいいですか? 〜のために 。たとえば、上記のコードで「First」と「Employee」と「給与」を渡さなければならない場合は、どうすればよいですか?
これを一般化するにはどうすればよいですか?
ご協力いただきありがとうございます。
EDIT - 私は私のポストで明らかにされていないので、私は再び
を言い換えるてみましょう私は、この
l | t | r | b | x | y | w_text | l_id
-------------------------------------------------------------
70 | 314 | 141 | 328 | 1 | 5 | First | 4
149 | 318 | 194 | 328 | 2 | 5 | Employe| 4
204 | 311 | 254 | 326 | 3 | 5 | John | 4
264 | 311 | 325 | 326 | 4 | 5 | Smith | 4
1924 | 310 | 2000 | 329 | 5 | 5 | First | 5
70 | 341 | 109 | 355 | 1 | 6 | step | 5
115 | 340 | 130 | 355 | 2 | 6 | of | 5
136 | 340 | 175 | 355 | 3 | 6 | Linq | 5
185 | 339 | 320 | 356 | 4 | 6 | Last | 6
70 | 394 | 101 | 411 | 1 | 8 | Employe| 6
114 | 390 | 199 | 405 | 2 | 8 | John | 6
210 | 390 | 269 | 405 | 3 | 8 | Doe | 6
のように私は私の手に持っている唯一の検索条件に見えるデータテーブルを持っているように見えます'W_Text'です。私はユニークなフレーズ "First Employee"を検索したいと思います.1つのl_id(この場合はl_id = 4)に "First"と "Employee"の両方の単語があります。 「First」と「Employee」を別々に使用すると、私の目的を解決しない大きなデータセットが得られます。私の目標は、私が「最初の従業員」SQL用語で
l | t | r | b | x | y | w_text | l_id
-------------------------------------------------------------
70 | 314 | 141 | 328 | 1 | 5 | First | 4
149 | 318 | 194 | 328 | 2 | 5 | Employe| 4
204 | 311 | 254 | 326 | 3 | 5 | John | 4
264 | 311 | 325 | 326 | 4 | 5 | Smith | 4
を検索する場合は、以下のユニークなデータセットを取得することで、これは
Select * From Table where l_id in (Select l_id from Table where W_Text in ('First','Employee') group by l_id having count(l_id) > 1)
私はによって助けられた(言及している上記のコードに似ています良いサマリアン)完璧に動作し、私に上記のデータセットを返します。問題は「First Employee」だけで動作することです。私は「Linqの第一歩」と言って検索に困っている。検索フレーズは実行時にプログラムに渡され、いつまでの言葉がどれくらいである可能性があります。私はWheresを分離しようとしましたが、 'having'条件のミスとデータセット全体が再び返されます。
私はあなたにこの問題を私に親切に助けてください。私はLinqにとってとても新しいので、自分のやり方で働こうとしています。その間、私が得ることができるどんな助けも高く評価されるでしょう。ありがとう。
---(誰かの助けを借りて)EDITこれは、このコードを使用して作業してしまった
List<string> wTextFilter = new List<string>();
foreach (string sf in strInputString.Split(' ')) //array by splitting on white space
{
wTextFilter.Add(sf);
}
// Get all Id's that satisfy all conditions:
List<int> results = dtResult.AsEnumerable()
// Get all Id's:
.Select(dataRow => dataRow.Field<int>("l_id"))
// Filter the Id's :
.Where(id =>
// the Id should be greater than one.
id > 1 &&
// check if all W_Text entries has a record in the datatable with the same Id.
wTextFilter.All(W_Text => dtResult.AsEnumerable().Any(dataRow => dataRow.Field<string>("W_Text") == W_Text && dataRow.Field<int>("l_id") == id)))
.Distinct().ToList();
// Get all datatable rows filtered by the list of Id's.
dtCopy = dtResult.AsEnumerable().Where(dataRow => results.Contains((dataRow.Field<int>("l_id")))).CopyToDataTable();
あなた自身のロジックマンを使用してください! –
あなたは何が必要なのかは不明ですが、 '.Where(condition1).Where(condition2)'のようにWhereの呼び出しを連鎖させることができます。 – vesan
アドバイスをいただき、ありがとうございます。私はそれを解読することができないので、専門家のアドバイスのために来た! – DevNovice