Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将参数传递给实体框架中的可重用表达式_C#_Entity Framework_Linq - Fatal编程技术网

C# 将参数传递给实体框架中的可重用表达式

C# 将参数传递给实体框架中的可重用表达式,c#,entity-framework,linq,C#,Entity Framework,Linq,我想通过变量y声明和重用带有过滤器的表达式。在一种方法中,我有如下内容: Expression<Func<Item, int, bool>> exFilter = (x, y) => x.Item.Id == y; 问:如何将参数传递给exFilter?我想按列表(x)中的每个项目进行筛选 这只是我想弄明白的一个例子。问题和查询要复杂得多 您可以尝试以下方法: public class Item { public Item(String str, Int3

我想通过变量y声明和重用带有过滤器的表达式。在一种方法中,我有如下内容:

Expression<Func<Item, int, bool>> exFilter = (x, y) => x.Item.Id == y;
问:如何将参数传递给exFilter?我想按列表(x)中的每个项目进行筛选


这只是我想弄明白的一个例子。问题和查询要复杂得多

您可以尝试以下方法:

public class Item
{
    public Item(String str, Int32 @int)
    {
        this.StrValue = str;
        this.IntValue = @int;
    }
    public String StrValue { get; }
    public Int32 IntValue { get; }
    public override string ToString() => 
        $"{this.IntValue} = '{this.StrValue}'";
}


public static class ExpressionExtensions
{
    public static Expression<Func<TItem, TResult>> Curry<TItem, TCurry, TResult>(
        this Expression<Func<TItem, TCurry, TResult>> function,
        TCurry value)
    {
        if (function == null)
            throw new ArgumentNullException(paramName: nameof(function));

        var itemParameter = Expression.Parameter(typeof(TItem));
        var valueConstant = Expression.Constant(value);

        return Expression.Lambda<Func<TItem, TResult>>(
            Expression.Invoke(
                function,
                new Expression[]
                {
                    itemParameter,
                    valueConstant
                }),
            new[] { itemParameter });
    }
}
Expression<Func<Item, bool>> exFilter(int y){ return (x) => x.item.Id == y;}
int paramY = 456;
return context.Item.Select(exFilter(paramY))
公共类项目
{
公共项(字符串str,Int32@int)
{
this.StrValue=str;
this.IntValue=@int;
}
公共字符串StrValue{get;}
公共Int32 IntValue{get;}
公共重写字符串ToString()=>
$“{this.IntValue}='{this.StrValue}'”;
}
公共静态类表达式扩展
{
公共静态表达式咖喱(
这个表达式函数,
t货币价值)
{
if(函数==null)
抛出新ArgumentNullException(paramName:nameof(函数));
var itemParameter=表达式.参数(typeof(TItem));
var valueConstant=表达式常数(值);
返回表达式.Lambda(
表达式.调用(
功能,
新表达式[]
{
itemParameter,
价值常数
}),
新[]{itempareter});
}
}

var items=new[]
{
新项目(“一”,1),
新项目(“2”,2),
新项目(“再次两次”,2),
};
表达式谓词=(项,intValue)=>
item.IntValue==IntValue;
var curriedPredicate=谓词.Curry(2);
var筛选=项目
.AsQueryable()
.Where(当前医嘱)
.ToArray();
foreach(过滤后的变量项)
{
控制台写入线(项目);
}
您可以使用来重用已有的表达式。以下是一个例子:

var result =
    context.Item //The DbSet
    .AsExpandable() //This method is defined in LinqKit and allows for expression expansion
    .Where(x => exFilter.Invoke(x, 2)) //LinqKit will know how to translate this into an expression
    .ToList();

我在这里使用值2作为示例。

您可以像这样重写代码:

public class Item
{
    public Item(String str, Int32 @int)
    {
        this.StrValue = str;
        this.IntValue = @int;
    }
    public String StrValue { get; }
    public Int32 IntValue { get; }
    public override string ToString() => 
        $"{this.IntValue} = '{this.StrValue}'";
}


public static class ExpressionExtensions
{
    public static Expression<Func<TItem, TResult>> Curry<TItem, TCurry, TResult>(
        this Expression<Func<TItem, TCurry, TResult>> function,
        TCurry value)
    {
        if (function == null)
            throw new ArgumentNullException(paramName: nameof(function));

        var itemParameter = Expression.Parameter(typeof(TItem));
        var valueConstant = Expression.Constant(value);

        return Expression.Lambda<Func<TItem, TResult>>(
            Expression.Invoke(
                function,
                new Expression[]
                {
                    itemParameter,
                    valueConstant
                }),
            new[] { itemParameter });
    }
}
Expression<Func<Item, bool>> exFilter(int y){ return (x) => x.item.Id == y;}
int paramY = 456;
return context.Item.Select(exFilter(paramY))

context.Item
是数据库集吗?
exFilter.Where(exFilter)
是什么意思?是的,它是数据库集。这只是一个过滤器,exFilter是其中的表达式。但它没有意义。如何将
Where
应用于
exFilter
?你能修改一下问题中的代码吗?我想代码还可以。可以在何处应用exFilter。我试过了——它奏效了。但是我不知道如何传递参数。