Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 将表达式参数作为LINQ中的属性添加到实体_C#_Linq_Linq To Entities_Entity Framework 6_Expression Trees - Fatal编程技术网

C# 将表达式参数作为LINQ中的属性添加到实体

C# 将表达式参数作为LINQ中的属性添加到实体,c#,linq,linq-to-entities,entity-framework-6,expression-trees,C#,Linq,Linq To Entities,Entity Framework 6,Expression Trees,使用EF6,如何将给定的表达式参数绑定到现有的select表达式,而不必使用表达式树重写每个属性绑定 public IEnumerable<RowModel> GetRowModels(Expression<Func<Row, string>> textExpr) { return from row in MyDatabaseContext.MyTable select new RowModel {

使用EF6,如何将给定的
表达式
参数绑定到现有的select表达式,而不必使用表达式树重写每个属性绑定

public IEnumerable<RowModel> GetRowModels(Expression<Func<Row, string>> textExpr)
{
    return from row in MyDatabaseContext.MyTable
           select new RowModel
           {
               RowID = row.ID,
               CreatedDate = row.CreatedDate,
               AnotherProperty = row.AnotherProperty,
               Text = textExpr, // how do I bind this expression?
               Value = row.OtherStuff.Where(os => os.ShouldUse).Select(os => os.Value).FirstOrDefault(),
               AnotherValue = row.OtherStuff.Where(os => os.ShouldUseAgain).Select(os => os.Value).FirstOrDefault()
           };
}
public IEnumerable GetRowModels(表达式textExpr)
{
从MyDatabaseContext.MyTable中的行返回
选择新的行模型
{
RowID=row.ID,
CreatedDate=row.CreatedDate,
AnotherProperty=行。AnotherProperty,
Text=textexxpr,//如何绑定此表达式?
Value=row.OtherStuff.Where(os=>os.ShouldUse)。选择(os=>os.Value)。FirstOrDefault(),
AnotherValue=row.OtherStuff.Where(os=>os.shouldUseReach)。选择(os=>os.Value)。FirstOrDefault()
};
}

这里需要的是一个组合多个表达式的方法。具体来说,我们想要的是一种方法,获取一个映射值的表达式,然后还接受一个表达式,该表达式接受第一个表达式的输入和第一个表达式的输出,并计算一个新值

作为该方法的实现,我们可以用第一个函数的主体替换“第一个函数的结果”的所有实例;在此之后,需要做的就是确保两个表达式使用相同的
参数
实例

public static Expression<Func<TFirstParam, TResult>>
    Combine<TFirstParam, TIntermediate, TResult>(
    this Expression<Func<TFirstParam, TIntermediate>> first,
    Expression<Func<TFirstParam, TIntermediate, TResult>> second)
{
    var param = Expression.Parameter(typeof(TFirstParam), "param");

    var newFirst = first.Body.Replace(first.Parameters[0], param);
    var newSecond = second.Body.Replace(second.Parameters[0], param)
        .Replace(second.Parameters[1], newFirst);

    return Expression.Lambda<Func<TFirstParam, TResult>>(newSecond, param);
}
至于使用该功能;这很简单。我们在您的
textExpression
上调用
Combine
,然后我们可以创建一个lambda,接受第一个表达式的行和文本结果作为参数。这使您可以编写一个与现有lambda几乎完全相同的lambda,但可以使用text参数指定
text
值:

public IEnumerable<RowModel> GetRowModels(
    Expression<Func<Row, string>> textExpr)
{
    return MyDatabaseContext.MyTable.Select(
        textExpr.Combine((row, text) => new RowModel
    {
        RowID = row.ID,
        CreatedDate = row.CreatedDate,
        AnotherProperty = row.AnotherProperty,
        Text = text, // how do I bind this expression?
        Value = row.OtherStuff.Where(os => os.ShouldUse)
            .Select(os => os.Value).FirstOrDefault(),
        AnotherValue = row.OtherStuff.Where(os => os.ShouldUseAgain)
            .Select(os => os.Value).FirstOrDefault()
    }));
}
public IEnumerable GetRowModels(
表达式(文本表达式)
{
返回MyDatabaseContext.MyTable.Select(
textExpr.Combine((行,文本)=>新建行模型
{
RowID=row.ID,
CreatedDate=row.CreatedDate,
AnotherProperty=行。AnotherProperty,
Text=Text,//如何绑定此表达式?
Value=row.OtherStuff.Where(os=>os.ShouldUse)
.Select(os=>os.Value).FirstOrDefault(),
AnotherValue=row.OtherStuff.Where(os=>os.shouldUseReach)
.Select(os=>os.Value).FirstOrDefault()
}));
}

在您的示例中,什么是
其他东西
。不清楚你在问什么。@BenRobinson
OtherStuff
与这个问题有什么关系?显然,这一行已经开始工作了。
OtherStuff
只是为了说明我为什么要避免使用表达式树构建所有属性绑定。
public IEnumerable<RowModel> GetRowModels(
    Expression<Func<Row, string>> textExpr)
{
    return MyDatabaseContext.MyTable.Select(
        textExpr.Combine((row, text) => new RowModel
    {
        RowID = row.ID,
        CreatedDate = row.CreatedDate,
        AnotherProperty = row.AnotherProperty,
        Text = text, // how do I bind this expression?
        Value = row.OtherStuff.Where(os => os.ShouldUse)
            .Select(os => os.Value).FirstOrDefault(),
        AnotherValue = row.OtherStuff.Where(os => os.ShouldUseAgain)
            .Select(os => os.Value).FirstOrDefault()
    }));
}