さまざまな理由から、ユーザーは列と値の選択に基づいてデータベースから項目を選択できる必要があります。私はテーブルを持っている場合、例えば、:LINQ動的な列と値を選択
Name | Specialty | Rank
-------+-----------------+-----
John | Basket Weaving | 12
Sally | Basket Weaving | 6
Smith | Fencing | 12
ユーザは、1,2、またはそれ以上の列と、それらが異なっていてもよく、要求列を要求することができます。 Specialty == Basket Weaving
とRank == 12. What I do currently is gather the user's request and create a list of
KeyValuePair where the
キーis the column name and the
[値]列の目標値であり、たとえば、ユーザーがエントリを要求することができる:KeyValuePair
年代のこのリストを使用して
class UserSearch
{
private List<KeyValuePair<string, string> criteria = new List<KeyValuePair<string, string>>();
public void AddTerm(string column, string value)
{
criteria.Add(new KeyValuePair<string, string>(column, value);
}
public void Search()
{
using (var db = new MyDbContext())
{
// Search for entries where the column's (key's) value matches
// the KVP's value.
var query = db.MyTable.Where(???);
}
}
}
/* ... Somewhere else in code, user adds terms to their search
* effectively performing the following ... */
UserSearch search = new UserSearch();
search.Add("Specialty", "Basket Weaving");
search.Add("Rank", "12");
、どのように私は最も簡潔にどのデータベース項目を選択することができますすべての基準に一致しますか?
using (var db = new MyDbContext)
{
// Where each column name (key) in criteria matches
// the corresponding value in criteria.
var query = db.MyTable.Where(???);
}
編集:私はそれを助けることができる場合は、未処理のSQLの代わりにEntityFrameworkを使用したいと思います。
更新3:近づいています。私はテーブルからすべての値を をダウンロードしたら、LINQを使用する方法を発見しました。これは明らかにテーブルの中のすべてを ダウンロードするので、理想的ではありません。だから私は最後のステップはどこの方法を理解することだと思います 毎回テーブル全体をダウンロードする必要はありません。
行ごとにテーブル
db.MyTable.ToList().Where(e => ...
で、私は列が基準に一致した場合に相当boolsのリストを作る:ここで私がやっていることの説明があります。
criteria.Select(c => e.GetType()?.GetProperty(c.Key)?.GetValue(e)?.ToString() == c.Value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Basically just gets the value of specific column
by string
それから私は、このブールリストにすべて該当
.All(c => c == true)
完全なコードの例であるかどうかを確認する以下の通りです:私の問題はであるかのように思える
// This class was generated from the ADO.NET Entity Data Model template
// from the database. I have stripped the excess stuff from it leaving
// only the properties.
public class MyTableEntry
{
public string Name { get; }
public string Specialty { get; }
public string Rank { get; }
}
class UserSearch
{
private List<KeyValuePair<string, string> criteria = new List<KeyValuePair<string, string>>();
public void AddTerm(string column, string value)
{
criteria.Add(new KeyValuePair<string, string>(column, value);
}
public async Task<List<MyTableEntry>> Search()
{
using (var db = new MyDbContext())
{
var entries = await db.MyTable.ToListAsync();
var matches = entries.Where(e => criteria.Select(c => e.GetType()
?.GetProperty(c.Key)
?.GetValue(e)
?.ToString() == c.Value)
.All(c => c == true));
return matches.ToList();
}
}
}
このコードセグメント:
e.GetType()?.GetProperty(c.Key)?.GetValue(e)?.ToString()
私は表現木に慣れていないので、おそらく答えがそれらにあるでしょう。ダイナミックLINQも試してみます。
私はこのポストを考えます助けてくれるでしょう。http://stackoverflow.com/questions/821365/how-to-convert-a-string-to-its-equivalent-expression-tree あなたが望む文字列をWhere実際に動作する式に変換します。 – Dylan
私は、SQLで不要な比較をしているという懸念を軽減するために、linqから生成されたSQLを含めるように答えを更新しました。 – Jakotheshadows
まだ解決していますか? –