简体   繁体   English

实体框架 - 组合两行数据

[英]Entity Framework - Combining data on two rows

I have two tables. 我有两张桌子。

Orders
OrderID | UserID | OrderTotal
1       |    1   |  100
2       |    2   |  110
3       |    1   |  120

Users
UserId | ProprtyType | PropertyValue
1      |     1       |    Kevin
1      |     2       |    Nolan
1      |     1       |    FirstName
1      |     2       |    Surname

Using the following Query 使用以下查询

var query = from orders in context.Orders
join users in context.Users on orders.UserID equals user.UserID
where userData.Type == 211 || userData.Type == 212

1       |    1   |  100 | Kevin
1       |    1   |  100 | Nolan
2       |    2   |  110 | FirstName
2       |    2   |  110 | Surname
3       |    1   |  120 | Kevin
3       |    1   |  120 | Nolan

Is it possible in Entity frame work to combine the results so it returns the following 是否可以在实体框架工作中组合结果,以便返回以下内容

1       |    1   |  100 | Kevin     | Nolan
2       |    2   |  110 | FirstName | Surname
3       |    1   |  120 | Kevin     | Nolan

Or 要么

1       |    1   |  100 | Kevin Nolan
2       |    2   |  110 | FirstName Surname
3       |    1   |  120 | Kevin Nolan

Thanks 谢谢

That Users table is really badly designed, IMO. 用户表真的很糟糕,IMO。 If you keep the table layout, please rename it UserProperties . 如果保留表格布局,请将其重命名为UserProperties Also, your example code doesn't match your table layout or the suggested results. 此外,您的示例代码与您的表格布局或建议的结果不匹配。

That being said, you can do something like this: 话虽这么说,你可以做这样的事情:

var firstnames = from user in context.Users
                 where user.PropertyType == 1
                 select new { Id = user.UserID,
                              Firstname = user.PropertyValue };

var lastnames = from user in context.Users
                where user.PropertyType == 2
                select new { Id = user.UserID,
                             Lastname = user.PropertyValue };

var users = from fn in firstnames
            join ln in lastnames on fn.Id equals ln.Id
            select new { Id = fn.Id,
                         Firstname = fn.Firstname,
                         Lastname = ln.Lastname };

var query = from order in context.Orders
            join user in users on order.UserID equals user.Id
            select new { /*what you need to select*/ };

One good thing about Entity Framework is that the actual SQL queries are deferred as long as possible. 实体框架的一个好处是尽可能延迟实际的SQL查询。 So just querying for firstnames and lastnames will not generate a database connection. 因此,仅查询firstnames和lastnames将不会生成数据库连接。 That comes later. 那是后来的事。 Unfortunately, your table design makes it really difficult to utilize those benefits of EF. 不幸的是,您的桌面设计使得利用EF的这些优势变得非常困难。

Agree with atornblad's answer, but just wanted to point out that you don't need to rely on deferred execution to get the first and last names. 同意atornblad的回答,但只是想指出你不需要依赖延迟执行来获得名字和姓氏。 You can join to the same table twice, similar to how you would alias the table to join to it multiple times in a sql query: 您可以两次加入同一个表,类似于在SQL查询中多次将表连接到它的别名:

var query = from o in Orders
            join fn in Users on o.UserId equals fn.UserId
            join ln in Users on fn.UserId equals ln.UserId
            where fn.PropertyType == 1 && ln.PropertyType == 2
            select new 
            { 
                OrderId = o.OrderId, 
                UserId = fn.UserId, 
                Total = o.OrderTotal, 
                FirstName = fn.PropertyValue, 
                LastName = ln.PropertyValue 
            };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM