EF中 GroupJoin 与 Join

数据:

GroupJoin: 返回左表所有数据

using (tempdbEntities context = new tempdbEntities())
            {
                var query = context.P.GroupJoin(context.S, p => p.PID, s => s.PID,
                    (p, s) => new { p.PNAME, c = s.Count() });
                foreach (var obj in query)
                {
                    Console.WriteLine(
                        "{0} - {1}",
                        obj.PNAME,
                        obj.c
                        );
                }
                Console.WriteLine("数量:" + query.Count());
                Console.ReadLine();
            }

结果:

Join:返回交集

using (tempdbEntities context = new tempdbEntities())
            {
                var query = context.P.Join(context.S, p => p.PID, s => s.PID,
                    (p, s) => new { p.PNAME, s.SNAME});
                foreach (var obj in query)
                {
                    Console.WriteLine(
                        "{0} - {1}",
                        obj.PNAME,
                        obj.SNAME
                        );
                }
                Console.WriteLine("数量:" + query.Count());
                Console.ReadLine();
            }

结果:

 

转载于:https://www.cnblogs.com/jasonlai2016/p/10820311.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Entity Framework Core (EF Core) allows you to perform inner join operation between two or more tables using LINQ queries or query syntax. Here is an example of performing inner join using LINQ queries: ```csharp var result = from emp in context.Employees join dept in context.Departments on emp.DepartmentId equals dept.DepartmentId select new { EmployeeName = emp.Name, DepartmentName = dept.Name }; ``` In this example, we are joining two tables `Employees` and `Departments` based on the `DepartmentId` column. The result will contain a collection of anonymous objects with properties `EmployeeName` and `DepartmentName`. Here is an example of performing inner join using method syntax: ```csharp var result = context.Employees .Join(context.Departments, emp => emp.DepartmentId, dept => dept.DepartmentId, (emp, dept) => new { EmployeeName = emp.Name, DepartmentName = dept.Name }) .ToList(); ``` In this example, we are performing inner join using `Join` method. We are passing four parameters to the `Join` method - first parameter is the inner collection (`context.Departments`), second parameter is the outer collection (`context.Employees`), third parameter is the key selector for the inner collection (`dept => dept.DepartmentId`), fourth parameter is the key selector for the outer collection (`emp => emp.DepartmentId`), and the fifth parameter is the result selector (`(emp, dept) => new {...}`). The `ToList()` method is used to execute the query and return the results as a list. I hope this helps! Let me know if you have any further questions.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值