【问题标题】:LINQ Distinct rows with multiple joinsLINQ 具有多个连接的不同行
【发布时间】:2017-01-27 10:49:26
【问题描述】:

我在尝试获取所有不同的行时遇到了麻烦,作为来自使用 linq 连接的多个表的信息,并且只返回每个产品的单行数据。连接为每个产品返回多行

第一版

    select DISTINCT * from z_product as zp
inner join orders_products op on zp.ProductId = op.products_id
inner join products p on op.products_id = p.products_id
inner join orders o on o.orders_id = op.orders_id
inner join product_design d on d.products_id = p.products_id
where tray = 35 
group by zp.Id

我想要的那种, 结果 2 个产品包含 Mysql Workbench 中的所有信息

第二版

尝试在外部不分组的情况下获取信息,但我需要列出我需要的所有列。我还不想做的事情。

我必须列出每一列吗? 我如何在 LINQ 中做到这一点?

    select  ass.* from z_product as zass LEFT JOIN ( SELECT zp.* from z_product as zp
    left join orders_products op on zp.ProductId = op.products_id
    left join products p on op.products_id = p.products_id
    left join orders o on o.orders_id = op.orders_id
    left join product_design d on d.products_id = p.products_id
    left join orders_products_attributes a on a.orders_products_id  =  op.orders_id
    group by zp.Id) ass
    ON ass.Id = zass.Id
    where zass.Tray=35

我想要的那种, 结果 2 个产品包含 Mysql Workbench 中的所有信息

what i do want

So i want to get something like this;
 [What i want in LINQ][1]

Attempt version 1 in LINQ

                var orderProducts = (from zp in SqlContext.z_product
                    join op in SqlContext.orders_products on zp.ProductId equals op.products_id
                    join p in SqlContext.products on op.products_id equals p.products_id
                    join o in SqlContext.orders on op.orders_id equals o.orders_id
                    join d in SqlContext.product_design on p.products_id equals d.products_id
                    join a in SqlContext.orders_products_attributes on op.orders_id equals
                    a.orders_products_id
                    where zp.Tray == SelectedTray.Id
                    group new Product
                    {
                        OrdersProduct = op,
                        Product = p,
                        Order = o,
                        AreaProduct = zp,
                        ProductDesign = d,
                        OrdersProductsAttributes = a,
                    } by zp.Id
                    into product
                    select product);

what i dont want

尝试使用综合? linq 将产生产品组。每个组都有一个对象列表。我只想要2个产品。

2 个组,每个组是其项目的列表。我只想拥有 2 个产品。

【问题讨论】:

  • 如果您有一个产生所需结果的有效 SQL 查询,则只发布该查询(并确保包含列别名,包括 *)。还要指定您的 ORM(LinqToSQL、EF(版本)、EF Core 等)、模型类和关系。
  • 它们都工作正常,但我不确定它们的完整性。我想将 EF6 与没有关系的现有数据库一起使用。 “包括列别名,包括 *) 是什么意思。”一切都在那里。
  • @IvanStoev 为什么我的问题是-1? ORM 甚至不应该在这里使用 c# 表达式。别名都是他们的等等。您的输入和标记无效?
  • 我不能说为什么,因为-1不是我的:)
  • @IvanStoev 道歉

标签: mysql linq join distinct


【解决方案1】:

答案:

所以我找到了它,在分组中使用 .First()。

            var orderProducts = (from zp in SqlContext.product
                             join op in SqlContext.orders_products on zp.ProductId equals op.orders_products_id
                             join p in SqlContext.products on op.products_id equals p.products_id
                             join o in SqlContext.orders on op.orders_id equals o.orders_id
                             join d in SqlContext.product_design on p.products_id equals d.products_id
                             join a in SqlContext.orders_products_attributes on op.orders_id equals
                             a.orders_products_id
                             where zp.Tray == tray
                             group new Product
                             {
                                 SiblingCount = (from oop in SqlContext.orders_products
                                                 where oop.orders_id == o.orders_id
                                                 select oop
                                         ).Count(),
                                 OrdersProduct = op,
                                 Product = p,
                                 Order = o,
                                 AreaProduct = zp,
                                 ProductDesign = d,
                                 OrdersProductsAttributes = a,
                             } by zp.Id
                             into product
                             select product.First());

从每个组中获取第一个元素。

【讨论】:

    猜你喜欢
    • 2015-01-20
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多