linq的多表查询

复制代码
public class P_type
        {
            public int p_type_id { get; set; }
            public int parent_id { get; set; }
        }

        public class Products
        {
            public int pid { get; set; }
            public int p_type_id { get; set; }

        }

        static void Main(string[] args)
        {
            List<P_type> list_P_type = new List<P_type>()
            {
                new P_type(){p_type_id=1,parent_id=0},
                new P_type(){p_type_id=2,parent_id=1},
                new P_type(){p_type_id=3,parent_id=1},
            };
            List<Products> list_Products = new List<Products>()
            {
                new Products(){p_type_id=2,pid=1},
                new Products(){p_type_id=2,pid=2},
                new Products(){p_type_id=3,pid=3},
                new Products(){p_type_id=1,pid=4},
            };
            //如何查询list_Products中的pid     在list_P_type中parent_id为1的p_type_id
            //我都不知道怎么描述了..直接来个SQL..
            //select pid from list_Products where list_Products.p_type_id in 
            //(select p_type_id from list_P_type where parent_id=1)
            
            var one = from a in list_P_type where a.p_type_id in (from b in list_P_type where b.parent_id==1 select b.p_type_id) select a.pid;
            //不能这样写啊- -0
        }
复制代码

 

var one = from a in list_Products  
          let t= list_P_type.Where(x=>x.parent_id == 1).Select(x=>x.p_type_id)
          where t.Contains(a.p_type_id)
          select a;
var one = list_Products.Where(a=>list_P_type.Where(x=>x.parent_id == 1).Select(x=>x.p_type_id).Contains(a.p_type_id));
  var two = list_P_type.Where(x => x.parent_id == 1).Join(list_Products, p => p.p_type_id, p => p.p_type_id, (p_type, p_product) => p_product);

 NHibernate各种查询

 

posted @   shiningrise  阅读(535)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2009-05-08 无火不进房间回家程序及说明(广茂达灭火机器人)
2009-05-08 广茂达机器人灭火程序(纯C语言版)
2007-05-08 070508_设置上次考试_考生管理_成绩批量录入
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css
点击右上角即可分享
微信分享提示