C# 向IQueryable添加一个Include扩展方法

Posted 吴晓阳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 向IQueryable添加一个Include扩展方法相关的知识,希望对你有一定的参考价值。

using System;
using System.Data.Objects;
using System.Linq;

namespace OutOfMemory.Codes
{
    /// <summary>
    /// Adds extension methods to the <see cref="IQueryable{T}"/> class.
    /// </summary>
    public static class QueryableExtender
    {
        /// <summary>
        /// Specifies the related objects to include in the query results.
        /// </summary>
        /// <typeparam name="T"><see cref="Type"/> of query to extend.</typeparam>
        /// <param name="this">The query to extend.</param>
        /// <param name="path">
        /// Dot-separated list of related objects to return in the query results.
        /// </param>
        /// <returns>
        /// A new <see cref="IQueryable{T}"/> with the defined query path.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// The parameter <paramref name="path"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// The parameter <paramref name="path"/> is empty. 
        /// </exception>
        public static IQueryable<T> Include<T>(this IQueryable<T> @this, string path)
        {
            var objectQuery = @this as ObjectQuery<T>;

            if (objectQuery != null)
            {
                return objectQuery.Include(path);
            }
            return @this;
        }
    }
}

 

DbExtensions.Include<T> Method (IQueryable<T>, String)

https://msdn.microsoft.com/en-us/library/system.data.entity.dbextensions.include

Namespace:  System.Data.Entity
Assembly:  EntityFramework (in EntityFramework.dll)

 

以上是关于C# 向IQueryable添加一个Include扩展方法的主要内容,如果未能解决你的问题,请参考以下文章

向 IQueryable 添加扩展方法,该方法对列表进行分页并在从 API 控制器方法返回时返回 json 对象

C# IQueryable和IEnumerable的区别

C# Entity Framework中的IQueryable和IQueryProvider详解

C# Entity Framework中的IQueryable和IQueryProvider详解

C#:使用 IQueryable 注入 DbContext 时,无法访问 ASP.NET Core 中的已处置对象

温故知新C#中 IEnumerable 与IQueryable