[英]How do I get a list of distinct rows from the result of multiple joins in Linq?
I have performed a few joins which have produced multiple columns with the same CustomerId.我执行了一些连接,这些连接产生了具有相同 CustomerId 的多个列。 I would like to filter the result to only display rows with distinct customer.
我想过滤结果以仅显示具有不同客户的行。 It does not matter which duplicate rows are removed.
删除哪些重复行并不重要。 Tried using group by but couldn't get it to work.
尝试使用 group by 但无法让它工作。 After this is filtered I would like to do a select to create new CustomerDTO.
过滤后,我想做一个选择来创建新的 CustomerDTO。 This is my code, any assistance is appreciated.
这是我的代码,任何帮助表示赞赏。
var query = (from customer in db.Customers
join person in db.People
on customer.PersonId equals person.BusinessEntityId
join phone in db.PeoplePhones
on person.BusinessEntityId equals phone.BusinessEntityId into phoneNumbers
from phone1 in phoneNumbers.DefaultIfEmpty()
join email in db.EmailAddresses
on person.BusinessEntityId equals email.BusinessEntityId into emailAddresses
from email1 in phoneNumbers.DefaultIfEmpty()
select new CustomerDTO
{
Id = customer.CustomerId,
Title = person.Title,
FirstName = person.FirstName,
LastName = person.LastName,
PhoneNumber = phoneNumbers.FirstOrDefault().PhoneNumber,
EmailAddress = emailAddresses.FirstOrDefault().Email,
Branch = customer.Branch.Name
}).ToList();
Can you give this a go and let me know if it works?你能试一试,让我知道它是否有效吗?
var query =
(
from customer in db.Customers
join person in db.People on customer.PersonId equals person.BusinessEntityId
join phone in db.PeoplePhones on person.BusinessEntityId equals phone.BusinessEntityId into phoneNumbers
join email in db.EmailAddresses on person.BusinessEntityId equals email.BusinessEntityId into emailAddresses
group new { customer, person, phoneNumbers, emailAddresses } by customer.CustomerId into gs
from g in gs.Take(1)
from pn in g.phoneNumbers.DefaultIfEmpty().Take(1)
from e in g.emailAddresses.DefaultIfEmpty().Take(1)
select new CustomerDTO
{
Id = g.customer.CustomerId,
Title = g.person.Title,
FirstName = g.person.FirstName,
LastName = g.person.LastName,
PhoneNumber = pn.PhoneNumber,
EmailAddress = e.Email,
Branch = g.customer.Branch.Name
}
).ToList();
I think you're on the right track using a group by.我认为您使用 group by 是在正确的轨道上。 Does something like this do what you need to?
像这样的事情做你需要做的吗?
var query = (from customer in db.Customers
join person in db.People on customer.PersonId equals person.BusinessEntityId
join phone in db.PeoplePhones on person.BusinessEntityId equals phone.BusinessEntityId into phoneNumbers
from phone1 in phoneNumbers.DefaultIfEmpty() join email in db.EmailAddresses on person.BusinessEntityId equals email.BusinessEntityId into emailAddresses
from email1 in phoneNumbers.DefaultIfEmpty()
group new {customer, person, phoneNumbers, emailAddresses} by customer.CustomerId into g
select new CustomerDTO
{
Id = g.FirstOrDefault().customer.CustomerId,
Title = g.FirstOrDefault().person.Title,
FirstName = g.FirstOrDefault().person.FirstName,
LastName = g.FirstOrDefault().person.LastName,
PhoneNumber = g.FirstOrDefault().phoneNumbers.FirstOrDefault().PhoneNumber,
EmailAddress = g.FirstOrDefault().emailAddresses.FirstOrDefault().Email,
Branch = g.FirstOrDefault().customer.Branch.Name
}).ToList();
Since it looks like you're using linq-to-sql or EntityFramework, This should be a good solution to do the grouping on the SQL server side.由于看起来您正在使用 linq-to-sql 或 EntityFramework,这应该是在 SQL 服务器端进行分组的一个很好的解决方案。 Otherwise it's totally possible to do the grouping in memory after executing the
.ToList()
否则完全有可能在执行
.ToList()
后在内存中进行分组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.