Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.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
Entity framework 为LINQ to Entities Where子句构建表达式树_Entity Framework_Linq To Entities_Expression Trees - Fatal编程技术网

Entity framework 为LINQ to Entities Where子句构建表达式树

Entity framework 为LINQ to Entities Where子句构建表达式树,entity-framework,linq-to-entities,expression-trees,Entity Framework,Linq To Entities,Expression Trees,我希望能够为LINQtoEntities查询(EF6)编写以下代码 如果没有wherective()方法,我将不得不在许多地方重复以下表达式: Repo.ContactRelations.Where(c => c.EndDate == null || c.EndDate > DateTime.Today) 我试图编写一个简单的扩展方法,但它给出了一个错误“LINQtoEntities无法识别方法whiteactive” public static IEnumerable where

我希望能够为LINQtoEntities查询(EF6)编写以下代码

如果没有wherective()方法,我将不得不在许多地方重复以下表达式:

Repo.ContactRelations.Where(c => c.EndDate == null || c.EndDate > DateTime.Today)
我试图编写一个简单的扩展方法,但它给出了一个错误“LINQtoEntities无法识别方法whiteactive”

public static IEnumerable wherective(此IEnumerable源代码),其中T:class,IMayExpire
{
返回source.Where(c=>c.EndDate==null | | c.EndDate>DateTime.Today);
}
在读了一些关于LINQtoEntities与LINQtoObject以及表达式树与Func的文章之后,我意识到我需要构建一个完整的表达式树来表达我的意图

我不知道怎么做,能帮我一下吗

public static IQueryable<T> WhereActive<T>(this IQueryable<T> source) where T : class, IMayExpire
{
    return source.Where(c => c.EndDate == null || c.EndDate > DateTime.Today);
}

使用System.Linq;
公共静态表达式IsActive(),其中T:class,IMayExpire
{
返回c=>c.EndDate==null | | c.EndDate>DateTime.Today;
}
var isActive=isActive();
var结果=Repo.ContactRelations.Where(isActive)
.选择(r=>new ContactModel
{
Id=r.Contact.Id,
FirstName=r.Contact.FirstName,
LastName=r.Contact.姓氏,
WorkEmail=r.Contact.WorkEmail
})

谢谢您的回复,但您的建议无效。我仍然收到相同的错误:LINQ to Entities无法识别方法'System.LINQ.IQueryable
1[ContactRelation]wherective[ContactRelation](System.LINQ.IQueryable
1[ContactRelation]),并且此方法无法转换为存储表达式。难道不需要将Where()表达式和我的自定义表达式重写为一个表达式,以便LINQ进行翻译吗?我可能不是100%正确,但除其他问题外,至少传递到whiteactive的参数与whiteactive中声明的参数不同。即,在调用ContactRelations.Wherective时,将有一个参数(例如,可以称为“i”)传递到Wherective,它与在…中声明的参数“c”无关。。。(基于我有限的理解)再次感谢你,但你的建议仍然无效。现在它抛出“Internal.NET Framework Data Provider error 1025”PS:我必须将IsActive的返回更改为Func,因为Where()接受Func,而不是表达式不接受。您需要使用
System.Linq.Queryable.Where(表达式)
而不是
System.Linq.Enumerable.Where(Func)
。您可能需要将表达式放在另一行…很抱歉延迟响应。你说得对,Aron,我不小心把这段代码放在了Select()块中,这就是它失败的原因。一旦我把代码移到外面,它就工作得很好。谢谢你的帮助。代码本身不应该导致那个错误。您是将它用作另一个查询的子查询还是类似的?
    public static IEnumerable<T> WhereActive<T>(this IEnumerable<T> source) where T : class, IMayExpire
    {
        return source.Where(c => c.EndDate == null || c.EndDate > DateTime.Today);
    }
public static IQueryable<T> WhereActive<T>(this IQueryable<T> source) where T : class, IMayExpire
{
    return source.Where(c => c.EndDate == null || c.EndDate > DateTime.Today);
}
var activeContactRelations = Repo.ContactRelations.WhereActive();
var result = activeContactRelations
            .Select(r => new ContactModel
            {
                Id = r.Contact.Id,
                FirstName = r.Contact.FirstName,
                LastName = r.Contact.Surname,
                WorkEmail = r.Contact.WorkEmail
            })
using System.Linq;

public static Expression<Func<T, bool>> IsActive<T>() where T : class, IMayExpire
{
    return c => c.EndDate == null || c.EndDate > DateTime.Today;
}

var isActive = IsActive<ContactRelation>();
var result = Repo.ContactRelations.Where(isActive)
            .Select(r => new ContactModel
            {
                Id = r.Contact.Id,
                FirstName = r.Contact.FirstName,
                LastName = r.Contact.Surname,
                WorkEmail = r.Contact.WorkEmail
            })