Linq to Entities如何从父级返回所有记录? [英] Linq to Entities How to return all records from Parent?

查看:98
本文介绍了Linq to Entities如何从父级返回所有记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Employee_ID int
员工名varchar(50)

Employee_ID int
Employee_Name varchar(50)

Sales_ID int
Employee_ID int
Sale_Amount money

Sales_ID int
Employee_ID int
Sale_Amount money

选择
*
从 员工emp
左外连接
销售s

s.Employee_ID = emp.Employee_ID

Select
*
From Employee emp
left outer join
Sales s
on
s.Employee_ID = emp.Employee_ID

1个Emp1 1 1 150.00
1 Emp1 2 1 500.00
2 Emp2 3 2 250.00
3 Emp3 NULL NULL NULL
4 Emp4 NULL NULL NULL
5 Emp5 4 5 700.00

1 Emp1 1 1 150.00
1 Emp1 2 1 500.00
2 Emp2 3 2 250.00
3 Emp3 NULL NULL NULL
4 Emp4 NULL NULL NULL
5 Emp5 4 5 700.00

            Dim query = From emp In entiites.Employee _
                    From sales In emp.Sales _
                    Select _
                        emp, _
                        sales

Linq到实体的结果(Employee_ID 3和4在哪里)

1:Emp1:150.0000
1:Emp1:500.0000
2:Emp2:250.0000
5:Emp5:700.0000

Linq To Entities Result (Where is Employee_ID 3 and 4)

1: Emp1: 150.0000
1: Emp1: 500.0000
2: Emp2: 250.0000
5: Emp5: 700.0000

            Dim query = From emp In entiites.Employee _
                    Group Join sales In entiites.Sales _
                    On emp.Employee_ID Equals sales.Employee.Employee_ID _
                    Into sales_grp = Group _
                    From Sel_SalesGrp In sales_grp.DefaultIfEmpty() _
                    Select _
                        emp, _
                        Sel_SalesGrp

然后我使用DefaultIfEmpty收到此错误:

LINQ to Entities无法识别方法"System.Collections.Generic.IEnumerable 1[m12Model.Sales] DefaultIfEmpty[Sales](System.Collections.Generic.IEnumerable 1 [m12Model.Sales])"方法,并且该方法无法转换为商店表达式.

LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable1[m12Model.Sales] DefaultIfEmpty[Sales](System.Collections.Generic.IEnumerable1[m12Model.Sales])' method, and this method cannot be translated into a store expression.

推荐答案

您需要使用"Into"和"DefaultIfEmpty()"关键字执行左联接:

You need to perform a left join by using the "Into" and "DefaultIfEmpty()" keywords:

Dim query = From emp In entities.Employee _
        Join sal In entities.Sales On emp.Employee_ID Equals sal.Employee_ID Into LeftJoinSal _
        From ljs In LeftJoinSal.DefaultIfEmpty() _
        Select _
            emp, _
            ljs

此站点显示了另一个简单示例: GeeksWithBlogs .尽管它在C#中.

This site shows another simple example: GeeksWithBlogs. Though it is in C#.

关键是您需要加入一个新名称,以防止它从您的第一个表中过滤结果(该表隐藏了不合并的行),然后使用DefaultIfEmpty()函数从该表中进行选择在没有连接的情况下提供默认值(空).

The key is that you need to join into a new name to prevent it from filtering the results from your first table (which hides the rows that don't join), then select from that using the DefaultIfEmpty() function, which provides a default (null) value in the cases where there is no join.

这篇关于Linq to Entities如何从父级返回所有记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆