.net core ef 连表查询

2024-05-01 09:44
文章标签 查询 core net ef 连表

本文主要是介绍.net core ef 连表查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Information和TypeInfo连表查询

类似:

select st.Title1,si.* from [Star_Information] si left join Star_TypeInfo st on si.typeId2=st.id

先在EfCoreDbContext.cs配置

  protected override void OnModelCreating(ModelBuilder builder){base.OnModelCreating(builder);//TypeInfo 连表查询配置builder.Entity<Information>().HasOne(si => si.TypeInfo).WithMany().HasForeignKey(si => si.TypeId2);// 跟TypeInfo表相关联的TypeId2}

在Information.cs实体类新增 public TypeInfo TypeInfo { get; set; } // 导航属性 

 

接口实现

  /// <summary>/// 获取信息列表/// </summary>/// <param name="channel">调用参数</param>/// <param name="pageSize">每页记录数。必须大于等于1</param>/// <param name="pageIndex">页码。首页从1开始,页码必须大于等于1</param>/// <param name="typeIds">分类Id集</param>/// <param name="typeInfoIds">多选分类Id集</param>/// <param name="auditIds">审核Id集</param>/// <param name="keywords">搜索关键词</param>/// <returns></returns>[HttpGet][Authorize("Manage_View")]public async Task<MessageDto> List(string channel, int pageSize, int pageIndex, string typeIds = "", string typeInfoIds = "", string auditIds = "", string keywords = ""){var parameter = (InformationParameter)await _adminMenuService.GetParameter(channel, new InformationParameter());if (parameter.Channel == ""){return new MessageDto { Message = "信息不存在" };}var where = PredicateBuilder.True<Information>();where = where.And(w => w.Channel == parameter.Channel && w.WebSiteId == _authorityModel.WebSite.Id);if (!string.IsNullOrEmpty(typeIds)){var lastPathId = typeIds.Split(',').LastOrDefault();where = where.And(w => w.TypeIdPath.Contains(lastPathId));}if (!string.IsNullOrEmpty(typeInfoIds)){var lastPathId = typeInfoIds.Split(',').LastOrDefault();where = where.And(w => w.TypeIdStrPath.Contains(lastPathId));}if (!string.IsNullOrEmpty(auditIds) && StringHelper.IsNumber(auditIds.Split(',').LastOrDefault())){var lastPathId = long.Parse(auditIds.Split(',').LastOrDefault());where = where.And(w => w.AuditStatus == lastPathId);}if (!string.IsNullOrEmpty(keywords)){//连表查询TypeInfo的标题where = where.And(w => w.TypeInfo.Title1.Contains(keywords));}//联表查询var infoList = await _informationService.GetListAsync(pageSize, pageIndex, where, order => order.OrderByDescending(o => o.IsTop).ThenBy(o => o.Sort.Length).ThenBy(o => o.Sort).ThenByDescending(o => o.ReleaseDate));//联表查询 这个可以直接查出已经关联的人才表//var infoList = await _informationService.GetListAsync(pageSize, pageIndex, where,//    order => order.OrderByDescending(o => o.IsTop).ThenBy(o => o.Sort.Length).ThenBy(o => o.Sort).ThenByDescending(o => o.ReleaseDate)//    , info => info.Include(i => i.TypeInfo));var totleCount = await _informationService.CountAsync(where);var listData = _mapper.Map<List<Information>, List<InformationDto>>(infoList);var typeInfoList = await _typeInfoService.GetListAsync(0, w => w.Channel == parameter.TypeChannel && w.WebSiteId == _authorityModel.WebSite.Id, order => order.OrderBy(o => o.Sort));foreach (var item in listData){//如果有关联简介的idif (item.TypeId2!=0){var typeInfo = await _typeInfoService.GetAsync(item.TypeId2);if (typeInfo!=null){                     item.Type2Name= typeInfo.Title1;item.Type2Img = typeInfo.PicURL1;}}if (!string.IsNullOrEmpty(item.TypeIdPath)){item.TypeIdPathName = new List<string>();var itemTypeList = typeInfoList.Where(w => item.TypeIdPath.Contains(w.Id.ToString()));foreach (var type in itemTypeList){item.TypeIdPathName.Add(type.Title1);if (item.Channel == "ScientificResearchPlatform"){string[] attrid = item.TypeIdPath.Split(',');if (type.DisplayMode == 0){if (item.TypeIdPath != ""){if (attrid.Length > 1){ item.Url = "http://103.236.254.221:20002/#/Platform/" + attrid[0] + "/" + attrid[1] + "/0/1";}}}else if (type.DisplayMode == 1){item.Url = "http://103.236.254.221:20002/#/NewsInfo/ScientificResearchPlatform/" + item.Id + "/" + attrid[0] + "";}else if (type.DisplayMode == 2){item.Url = "";}}}}}return new MessageDto { Success = true, Data = new { listData, totleCount } };}

这篇关于.net core ef 连表查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/951360

相关文章

房产证 不动产查询

陕西政务服务网(便民服务)陕西政务服务网(手机版?更直观)不动产权证书|不动产登记证明(电子证照)商品房合同备案查询权利人查询

通过高德api查询所有店铺地址信息

通过高德api查询所有店铺地址电话信息 需求:通过高德api查询所有店铺地址信息需求分析具体实现1、申请高德appkey2、下载types city 字典值3、具体代码调用 需求:通过高德api查询所有店铺地址信息 需求分析 查询现有高德api发现现有接口关键字搜索API服务地址: https://developer.amap.com/api/webservice/gui

SQL Server中,查询数据库中有多少个表,以及数据库其余类型数据统计查询

sqlserver查询数据库中有多少个表 sql server 数表:select count(1) from sysobjects where xtype='U'数视图:select count(1) from sysobjects where xtype='V'数存储过程select count(1) from sysobjects where xtype='P' SE

Windows中,.net framework 3.5安装

安装.net framework,目前已知2种方法,如下: 一、在MSDN下载对应的安装包,安装,这种可能无法安装成功,概率很大,不成功使用第二种方法,基本上没问题。 二、win8/8.1/10 下安装 .net framework 3.5.1: 1. 打开 win8/8.1/10 安装盘(这里指系统安装镜像文件),提取 sources\sxs 文件夹到 X:\sources\sxs (X代

Java中如何优化数据库查询性能?

Java中如何优化数据库查询性能? 大家好,我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编,也是冬天不穿秋裤,天冷也要风度的程序猿!今天我们将深入探讨在Java中如何优化数据库查询性能,这是提升应用程序响应速度和用户体验的关键技术。 优化数据库查询性能的重要性 在现代应用开发中,数据库查询是最常见的操作之一。随着数据量的增加和业务复杂度的提升,数据库查询的性能优化显得尤为重

BD错误集锦9——查询hive表格时出错:Wrong FS: hdfs://s233/user/../warehouse expected: hdfs://mycluster

集群环境描述:HDFS集群处于HA模式下,同时启动了YARN\JN\KAFKA\ZK。 现象: FAILED: SemanticException Unable to determine if hdfs://s233/user/hive/warehouse/mydb.db/ext_calllogs_in_hbase is encrypted: java.lang.IllegalArgument

ASP.Net.WebAPI和工具PostMan

1.WebAPI概述 1.1 WebAPI WebAPI 是一种传统的方式,用于构建和暴露 RESTUI风格的Web服务。它提供了丰富的功能和灵活性,可以处理各种HTTP请求,并支持各种数据格式,如JSON、XML等。 WebAPI使用控制器(Controllers)和动作方法(ActionMethods)的概念、通过路由配置将请求映射到相应的方法上。 开发人员可以使用各种属性和过滤器来处

MybatisPlus指定字段查询

一,上代码 QueryWrapper<Device> queryWrapper = Wrappers.query();queryWrapper.select("project_id as projectId,count(device_id) as total").in("project_id",projectIds).isNotNull("project_id").eq("del_flag",B

ORACLE 、达梦 数据库查询指定库指定表的索引信息

在Oracle数据库中,索引是一种关键的性能优化工具,通过它可以加快数据检索速度。在本文中,我们将深入探讨如何详细查询指定表的索引信息,以及如何利用系统视图和SQL查询来获取这些信息。 索引在数据库中的重要性 索引是一种数据结构,用于加快数据库表中数据的检索速度。它类似于书籍的目录,可以帮助数据库引擎快速定位数据行,特别是在大型数据集合下,其作用尤为显著。 查询指定表的索引信息 在Orac

【ASP.NET】 No 'Access-Control-Allow-Origin' header is present on the requested resource.

前端JS用XMLHttpRequest,请求后端数据。出现了No ‘Access-Control-Allow-Origin’ header is present on the requested resource. 我是使用的ASP.NET框架。 解决办法: 在Web.config文件相应地方添加: <?xml version="1.0" encoding="utf-8"?><confi