ASP.NET Core EF 查询获取导航属性值,使用Include封装

复制代码
     // 引用 using Microsoft.EntityFrameworkCore;
        // 摘要:
        //     Specifies related entities to include in the query results. The navigation property
        //     to be included is specified starting with the type of entity being queried (TEntity).
        //     Further navigation properties to be included can be appended, separated by the
        //     '.' character.
        //
        // 参数:
        //   source:
        //     The source query.
        //
        //   navigationPropertyPath:
        //     A string of '.' separated navigation property names to be included.
        //
        // 类型参数:
        //   TEntity:
        //     The type of entity being queried.
        //
        // 返回结果:
        //     A new query with the related data included.
        public static IQueryable<TEntity> Include<TEntity>([NotNullAttribute] this IQueryable<TEntity> source, [NotNullAttribute][NotParameterized] string navigationPropertyPath) where TEntity : class;
复制代码

core中提供的扩展方法Include有两个重载方法,我们这里使用第一个重载方法,传参数导航属性名字,返回IQueryable<TEntity>,多对多导航属性,二级导航属性需要用‘.’点分隔符连接,提供完整导航属性名称。

下面是我封装的扩展方法:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace System
{
    public static class IQueryableExtensions
    {
        /// <summary>
        /// 导航属性,参数:导航属性名称字符串,支持多表查询
        /// 多级导航属性:“属性名.属性名”  用‘.’连接
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="Properts"></param>
        /// <returns></returns>
        public static IQueryable<T> In<T>(this IQueryable<T> obj, params string[] Properts) where T : class
        {
            IQueryable<T> data = obj;
            foreach (var prop in Properts)
            {
                data = data.Include(prop);
            }
            return data;
        }

    }
}
View Code
复制代码
复制代码
public class FREEFUNC
    {
        [Key]
        public long FFID { get; set; }
        public Nullable<int> SCID { get; set; }
        [ForeignKey("FUNCDEFINE")]
        public int FID { get; set; }
        public virtual FUNCDEFINE FUNCDEFINE { get; set; }
    }

public partial class FUNCDEFINE
    {
        [Key]
        public int FID { get; set; }
        public string FNAME { get; set; }
        public int CANOPR { get; set; }
        public int ISMENU { get; set; }
        public int ISEDIT { get; set; }
        public Nullable<int> FUNCTYPE { get; set; }
        [ForeignKey("FUNCDEFINE")]
        public Nullable<int> HIGHFID { get; set; }
        public string ICON { get; set; }
        public string CONTROLLER { get; set; }
        public string ACTION { get; set; }

        public virtual FUNCDEFINE HIGHF { get; set; }
    }
复制代码

调用实例:

var list = FREEFUNCService.GetList().In("FUNCDEFINE", "FUNCDEFINE.HIGHF").ToList();

注:GetList()返回IQueryable<FREEFUNC>类型,IEnumerable<T>类型不支持Include方法,导航属性必须延迟查询时调用,最终生成连表查询sql语句。

另外不使用Include方法也可以获取导航属性,获得IQueryable对象延迟查询,再使用.Select查询时返回值中获取导航属性值,最终也会生成连表查询,foreach不支持。

.net core ef中 获取数据直接ToList() 导航属性为null。

posted @   hg一如既往  阅读(3531)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
历史上的今天:
2012-11-05 C# 批量图片合并工具(附源代码)
点击右上角即可分享
微信分享提示