如何扩展 iqueryable 添加include方法
1个回答
展开全部
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;
}
}
}
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;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询