LINQ具有多个连接的不同行

时间:2016-09-19 17:00:37

标签: mysql linq join distinct

我无法尝试获取所有不同的行,因为来自多个表的信息使用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件产品。

1 个答案:

答案 0 :(得分:0)

答案:

所以我发现它,在分组中使用.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());

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