如何在实体框架中实现多个包含? [英] How can I implement multiple Include in Entity Framework?

查看:104
本文介绍了如何在实体框架中实现多个包含?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架6。我有一个带有多个导航属性的Transaction对象。

I use Entity framework 6. I have a Transaction object with several navigation properties. It is easy to implement eager loading using multiple Include.

 var aa = db.Transactions.Include(p => p.Account).Include(p => p.Instrument);

如果要包含的字段是参数,如何实现相同的功能?

How can I implement the same if the fields to be included are parameters?

var aa = db.Transactions.IncludeMore(delegatesToBeIncluded);   

如果 delegatesToBeIncluded 为空,则不包含任何内容。

If delegatesToBeIncluded is null then there is nothing to be included.

https://stackoverflow.com/a/38823723/5852947

https://stackoverflow.com / a / 35889204/5852947 这也很有趣。

如何通过lambda

How to pass lambda 'include' with multiple levels in Entity Framework Core? This focuses on multiple level (I have one level)

https://stackoverflow.com/a/52156692/5852947 这也很有希望。

我应该往哪个方向走?

修订版1 :为什么需要这个?
将基于 aa 的元素创建新对象。我意识到在每次创建对象时,EF都会读取数据库(使用延迟加载)。它只有50毫秒,但重复了n次。
此函数在模板类中实现,因此事务处理也是一个参数。

Revision 1: Why I need this? Based on the elements of aa new objects will be created. I realized that at each object creation EF reads the DB (lazy loading is used). It is just 50 ms, but it is repeated n times. This function is implemented in a template class, so Transactions is also a parameter.

修订版2 :在完整代码中进行了过滤(分页)确切地说),然后ToList()结束。它是在模板函数中实现的棘手部分。 dbTableSelector 是一个委托: readonly Func< MainDbContext,DbSet< TDbTable>> dbTableSelector;

Revision 2: In the full code there is filtering (pagination to be exact), and ToList() at then end. The tricky part that it is implemented in a template function. dbTableSelector is a delegate: readonly Func<MainDbContext, DbSet<TDbTable>> dbTableSelector;

 var myList = dbTableSelector(db).Where(WhereCondition).
             Skip(numberOfSkippedRows).Take(PageSize).OrderBy(OrderByCondition).ToList();

之后,我将 myList 的每个元素转换为另一种目的。这是每个元素逐个激活惰性加载的地方。这就是为什么我尝试使用Include的原因。如果 dbTableSelector(db)返回交易,我必须在返回时包括其他元素,我们说仪器。因此,IncludeMore应该具有一个List参数,该参数定义要包含的字段。

After that I transform each element of myList to another type of object. This is where lazy loading is activated one by one for each element. That is why I try to use Include. If dbTableSelector(db) returns Transactions I have to Include different elements when it returns let us say Instruments. So IncludeMore should have a List parameter which defines the fields to be included.

推荐答案

以下是解决方案。它基于

Here is the solution. It is based on this.

public static class IQueryableExtensions
{
    public static IQueryable<T> IncludeMultiple<T, TProperty>(this IQueryable<T> query,
        Expression<Func<T, TProperty>>[] includeDelegates) where T : class
    {
        foreach (var includeDelegate in includeDelegates)
            query = query.Include(includeDelegate);
        return query;
    }
}

这是呼叫:

var pathsA = new Expression<Func<ViewTransaction, object>>[2] { p => p.Account, p => p.Instrument };
var pathsB = new Expression<Func<ViewTransaction, object>>[1] { p => p.Account};
var pathsC = Array.Empty<Expression<Func<ViewTransaction, object>>>();


var a = db.ViewTransactions.IncludeMultiple(pathsA).Single(e => e.Id == 100);

这篇关于如何在实体框架中实现多个包含?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆