gpt4 book ai didi

c# - 修改表达式>

转载 作者:行者123 更新时间:2023-11-30 20:33:04 25 4
gpt4 key购买 nike

我对表达式的处理不多,所以我不能说我的意图是否有意义。我环顾了互联网,但无济于事。

假设我有这样的方法

public async Task<T> GetFirstWhereAsync(Expression<Func<T, bool>> expression)
{
// this would be a call to 3rd party dependency
return await SomeDataProvider
.Connection
.Table<T>()
.Where(expression)
.FirstOrDefaultAsync();
}

从我的其他代码中我可以调用它,例如

private async Task<User> GetUser(Credentials credentials)
{
return await SomeDataSource
.GetFirstWhereAsync(u => u.UserName.Equals(credentials.UserName));
}

所以我将从我的 SomeDataProvider 接收到与给定表达式匹配的第一个 User


我的实际问题是我将如何着手修改 GetFirstWhereAsync 以便它将一些 SecretSauce 应用于传递给它的任何表达式?我可以在调用者中执行此操作,但那样会很丑陋而且没有多大乐趣。

所以如果我传入这样的表达式

u => u.UserName.Equals(credentials.UserName);
p => p.productId == 1;

我想将这些修改为

u => u.UserName.Equals(SecretSauce.Apply(credentials.UserName));
p => p.productId == SecrectSauce.Apply(1);

最佳答案

可以修改方法内部的表达式,但是有点复杂,需要具体情况具体分析。

下面的示例将处理从 x => x.Id == 1x => x.Id == SecretSauce.Apply(1) 的修改。 p>


class User
{
public int Id { get; set; }
public string Name { get; set;}

public override string ToString()
{
return $"{Id}: {Name}";
}
}

class SquareSauce
{
public static int Apply(int input)
{
// square the number
return input * input;
}
}

数据

User[] user = new[]
{
new User{Id = 1, Name = "One"},
new User{Id = 4, Name = "Four"},
new User{Id = 9, Name = "Nine"}
};

方法

User GetFirstWhere(Expression<Func<User, bool>> predicate)
{
//get expression body
var body = predicate.Body;

//get if body is logical binary (a == b)
if (body.NodeType == ExpressionType.Equal)
{
var b2 = ((BinaryExpression)body);
var rightOp = b2.Right;

// Sauce you want to apply
var methInfo = typeof(SquareSauce).GetMethod("Apply");

// Apply sauce to the right operand
var sauceExpr = Expression.Call(methInfo, rightOp);

// reconstruct equals expression with right operand replaced
// with "sauced" one
body = Expression.Equal(b2.Left, sauceExpr);

// reconstruct lambda expression with new body
predicate = Expression.Lambda<Func<User, bool>>(body, predicate.Parameters);
}
/*
deals with more expression type here using else if
*/
else
{
throw new ArgumentException("predicate invalid");
}

return user
.AsQueryable()
.Where(predicate)
.FirstOrDefault();
}

用法

Console.WriteLine(GetFirstWhere(x => x.Id == 2).ToString());

该方法会将 x => x.Id == 2 转换为 x => x.Id == SquareSauce.Apply(2) 并将产生:

4: Four

关于c# - 修改表达式<Func<T, bool>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40671500/

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