gpt4 book ai didi

c# - 具有多个/未知条件的动态 linq 查询

转载 作者:太空宇宙 更新时间:2023-11-03 15:32:42 24 4
gpt4 key购买 nike

<分区>

我希望实现一个系统,通过该系统可以“构建”条件,然后从数据库返回结果数据。目前,有一个存储过程可以动态生成 SQL 并执行它。这是我要删除的特定问题。

我的问题是我的标准中可以有多个字段,并且对于这些字段中的每一个,可能有 1 个或多个值,具有不同的潜在运算符。

例如,

from t in Contacts 
where t.Email == "email@domain.com" || t.Email.Contains ("mydomain")
where t.Field1 == "valuewewant"
where t.Field2 != "valuewedontwant"
select t

字段、条件和运算符存储在数据库中(和 List<FieldCriteria> )并且会是这样的(基于上面);

Email, Equals, "email@domain.com"
Email, Contains, "mydomain" Field1,
Equals, "valuewewant" Field2,
DoesNotEqual, "valuewedontwant"

new FieldCriteria
{
FieldName = "Email",
Operator = 1,
Value = "email@mydomain.com"
}

因此,使用我所拥有的信息,我希望能够构建具有任意数量条件的查询。我已经看到以前指向 Dynamic Linq 和 PredicateBuilder 的链接,但我无法将其视为我自己问题的解决方案。

如有任何建议,我们将不胜感激。

更新

根据有关 Dynamic Linq 的建议,我提出了一个非常基本的解决方案,使用一个具有 2 个字段和多个条件的运算符。目前在 LinqPad 中编码有点粗糙,但结果正是我想要的;

enum Operator
{
Equals = 1,
}

class Condition
{
public string Field { get; set; }
public Operator Operator { get; set;}
public string Value { get; set;}
}

void Main()
{
var conditions = new List<Condition>();

conditions.Add(new Condition {
Field = "Email",
Operator = Operator.Equals,
Value = "email1@domain.com"
});

conditions.Add(new Condition {
Field = "Email",
Operator = Operator.Equals,
Value = "email2@domain.com"
});

conditions.Add(new Condition {
Field = "Field1",
Operator = Operator.Equals,
Value = "Chris"
});

var statusConditions = "Status = 1";

var emailConditions = from c in conditions where c.Field == "Email" select c;
var field1Conditions = from c in conditions where c.Field == "Field1" select c;


var emailConditionsFormatted = from c in emailConditions select string.Format("Email=\"{0}\"", c.Value);
var field1ConditionsFormatted = from c in field1Conditions select string.Format("Field1=\"{0}\"", c.Value);

string[] conditionsArray = emailConditionsFormatted.ToArray();
var emailConditionsJoined = string.Join("||", conditionsArray);
Console.WriteLine(String.Format("Formatted Condition For Email: {0}",emailConditionsJoined));

conditionsArray = field1ConditionsFormatted.ToArray();
var field1ConditionsJoined = string.Join("||", conditionsArray);
Console.WriteLine(String.Format("Formatted Condition For Field1: {0}",field1ConditionsJoined));



IQueryable results = ContactView.Where(statusConditions);

if (emailConditions != null)
{
results = results.Where(emailConditionsJoined);
}

if (field1Conditions != null)
{
results = results.Where(field1ConditionsJoined);
}

results = results.Select("id");

foreach (int id in results)
{
Console.WriteLine(id.ToString());
}
}

用一条SQL生成;

-- Region Parameters
DECLARE @p0 VarChar(1000) = 'Chris'
DECLARE @p1 VarChar(1000) = 'email1@domain.com'
DECLARE @p2 VarChar(1000) = 'email2@domain.com'
DECLARE @p3 Int = 1
-- EndRegion
SELECT [t0].[id]
FROM [Contacts].[ContactView] AS [t0]
WHERE ([t0].[field1] = @p0) AND (([t0].[email] = @p1) OR ([t0].[email] = @p2)) AND ([t0].[status] = @p3)

和控制台输出:

Formatted Condition For Email: Email="email1@domain.com"||Email="email2@domain.com"
Formatted Condition For Field1: Field1="Chris"

只需要清理它并添加其他 Operator,它看起来不错。

到目前为止,如果有人对此有任何意见,我们将不胜感激

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com