(タイトルがこの質問を代表していない場合は、そのように言ってください、私は良い短い説明を思い付くことができませんでした)
Webアプリケーションに高度な検索機能を実装しようとしています。この検索は、多くの可能な組み合わせで検索できる必要があります。
図に示されているように、すべてのグループ(ANDまたはOR)には、等しいまたは等しくない別のグループまたは条件を含めることができます。この図の結果、where句は次のようになります。
WHERE (Application like '%User%' AND Host not like '%fjzhykjety%') OR (REMOTE_HOST like '%uykirlyy%' AND REMOTE_PORT not like '%55555%')
アプリケーション、ホスト、およびREMOTE_HOSTは文字列であり、REMOTE_PORTはnull許容のintです。また、検索可能なGUIDも持っています。
現時点では、このフロントエンド検索は角度で記述されており、次のjson(jsonはネストされたグループと条件に依存します)の例を示します。
{
"group": {
"operator": "OR",
"rules": [
{
"group": {
"operator": "AND",
"rules": [
{
"condition": "=",
"field": {
"name": "Application",
"type": "select"
},
"data": "User"
},
{
"condition": "<>",
"field": {
"name": "Host",
"type": "text"
},
"data": "fjzhykjety"
}
]
}
},
{
"group": {
"operator": "AND",
"rules": [
{
"condition": "=",
"field": {
"name": "REMOTE_HOST",
"type": "text"
},
"data": "uykirlyy"
},
{
"condition": "<>",
"field": {
"name": "REMOTE_PORT",
"type": "number"
},
"data": 55555
}
]
}
}
]
}
}
このデータを(REST APIを使用して).NETアプリケーションに送信し、データベースでクエリする方法が必要です。クエリを作成するために動的linqを調べましたが、jsonファイルの形式が常に同じではないため、C#でクラスを作成してこの検索用の動的linqクエリを作成するのは困難です。
この高度な検索を実装する最良の方法は何ですか?
以下は動的クエリの例です。また、クエリと組み合わせることができるlinqkitは優れた述語ビルダーです。
http://www.albahari.com/nutshell/linqkit.aspx https://github.com/scottksmith95/LINQKit
public List<CustomerPointsDetail> GetCustomerPointsDetails(
int customerId,
int? catalogRewardId = null,
long? couponId = null,
long? transactionId = null,
CustomerPointsDetailStatus? inStatus = null,
CustomerPointsDetailStatus? notInStatus = null,
bool? isPointsGreaterThanZero = null,
bool? isRemainingPointsGreaterThanZero = null,
CustomerPointsDetailOperationType? operationType = null,
DateTime? startDate = null,
DateTime? endDate = null,
bool isExpiredRecordsIncluded = false)
{
var query = this.customerPointsDetailRepository.Table.Where(cpd => cpd.CustomerId == customerId);
if (catalogRewardId.HasValue)
{
query = query.Where(cpd => cpd.CatalogRewardId == catalogRewardId);
}
if (couponId.HasValue)
{
query = query.Where(cpd => cpd.CouponId == couponId);
}
if (transactionId.HasValue)
{
query = query.Where(cpd => cpd.TransactionId == transactionId);
}
if (inStatus.HasValue)
{
query = query.Where(cpd => cpd.Status == inStatus);
}
if (notInStatus.HasValue)
{
query = query.Where(cpd => cpd.Status != notInStatus);
}
if (isRemainingPointsGreaterThanZero.HasValue)
{
if (isRemainingPointsGreaterThanZero.GetValueOrDefault())
{
query = query.Where(cpd => cpd.RemainingPoints.HasValue && cpd.RemainingPoints > 0);
}
else
{
query = query.Where(cpd => cpd.RemainingPoints.HasValue && cpd.RemainingPoints < 0);
}
}
if (isPointsGreaterThanZero.HasValue)
{
if (isPointsGreaterThanZero.GetValueOrDefault())
{
query = query.Where(cpd => cpd.Points > 0);
}
else
{
query = query.Where(cpd => cpd.Points < 0);
}
}
if (operationType.HasValue)
{
query = query.Where(cpd => cpd.OperationType == operationType);
}
if (!isExpiredRecordsIncluded)
{
query = query.Where(cpd => !cpd.PointsExpireDate.HasValue
|| (cpd.PointsExpireDate.HasValue && cpd.PointsExpireDate > DateTime.Now));
}
if (startDate.HasValue)
{
query = query.Where(cpd => cpd.CreateDate >= startDate);
}
if (endDate.HasValue)
{
query = query.Where(cpd => cpd.CreateDate <= endDate);
}
query = query.OrderBy(cpd => cpd.PointsExpireDate);
return query.ToList();
}
Linqkitサンプルはこちら
IQueryable<Product> SearchProducts (params string[] keywords)
{
var predicate = PredicateBuilder.False<Product>();
foreach (string keyword in keywords)
{
string temp = keyword;
predicate = predicate.Or (p => p.Description.Contains (temp));
}
return dataContext.Products.Where (predicate);
}
編集する
あなたはあなたのメソッドと式を与える式が必要です。 Linqkitと組み合わせることができます。
Expression<Func<Product, bool>> e1 = DynamicExpression.ParseLambda<Product, bool>("Type= \"Type_A\"");
OR
Expression<Func<Product, bool>> e1 = p => p.Type="Type_A";
プログラムでSearchProductを呼び出す前に
var expressions = new Dictionary<Expression<Func<Product, bool>>, bool>();
Expression<Func<Product, bool>> e1 = p => p.Type="Type_A";
Expression<Func<Product, bool>> e2 = p => p.Type="Type_B";
expressions.Add(e1,true);
expressions.Add(e2,true);
SearchProducts(expressions); //send expression list for evoluate
IQueryable<Product> SearchProducts (Dictionary<Expression<Func<Product, bool>>, bool> expressionList)
{
var predicate = PredicateBuilder.False<Product>();
foreach(expression in expressionList)
{
if(expression.Value) // AND
{
predicate = predicate.And(expression.Key);
}else // OR
{
predicate = predicate.Or(expression.Key);
}
}
.
.
}