简体   繁体   English

在linq中对实体使用自定义方法

[英]Using custom methods in linq to entities

I have a Person table in my database that has NationalId field. 我的数据库中有一个包含NationalId字段的Person表。 Is there any way to load all Persons with even NationalId , using Ef code first and Linq to entities , without loading all Person s to memory? 有没有办法先使用Ef code first并将Linq to entities加载Linq to entities ,甚至使用NationalId加载所有Person ,而不将所有Person加载到内存?

Somethings like: 诸如此类的东西:

public  bool IsEven(int number)
{
   return number % 2 == 0;
}

var context = new MyContext();
var personsWithEvenNationalId = context.Persons
                                       .Where(x=> IsEven(x.NationalId))
                                       .ToList();

You would have to do your check inline 您将需要内联检查

var personsWithEvenNationalId = context.Persons
                                       .Where(x=> x.NationalId%2 == 0)
                                       .ToList();

Linq to Entities doesn't know how to translate your custom method into SQL basically. Linq to Entities基本上不知道如何将自定义方法转换为SQL。 If you do need to use a custom method you would have to get Persons as enumerable and then use your custom method, ie 如果确实需要使用自定义方法,则必须使Persons枚举,然后使用自定义方法,即

var personsWithEvenNationalId = context.Persons
                                       .AsEnumerable()
                                       .Where(x=> IsEven(x.NationalId))
                                       .ToList();

But that's hardly ideal as it would load all Persons and then filter on IsEven 但这并不理想,因为它将加载所有Person,然后在IsEven上进行过滤

Edit: Thinking about it you could also create an extension method for IQueryable<Person> if you don't want to have to write it inline every time. 编辑:考虑一下,如果您不想每次都内联编写它,则还可以为IQueryable<Person>创建扩展方法。 Something like this where you build an Expression 这样的东西,你建立一个Expression

    public static IQueryable<Person> WhereEven(this IQueryable<Person> source, Expression<Func<Person, int>> property)
    {
        var expression = Expression.Equal(
            Expression.Modulo(
                property.Body,
                Expression.Constant(2)),
            Expression.Constant(0));

        var methodCallExpression = Expression.Call(typeof (Queryable),
            "where",
            new Type[] {source.ElementType},
            source.Expression,
            Expression.Lambda<Func<Person, bool>>(expression, property.Parameters));

        return source.Provider.CreateQuery<Person>(methodCallExpression);
    }

And to use it: 并使用它:

context.Persons.WhereEven(x => x.NationalId).ToList();

Rather than a function that does what you want, you'll need to have an function (or property, or field) that provides an Expression that does the projection that you want: 您需要具有一个提供执行所需投影的Expression的函数(或属性或字段),而不是执行所需功能的函数:

public static Expression<Func<int, bool>> IsEven()
{
    return number => number % 2 == 0;
}

You can now write: 您现在可以编写:

using(var context = new MyContext())
{
    var personsWithEvenNationalId = context.Persons
        .Select(x=> x.NationalId)
        .Where(IsEven())
        .ToList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM