如何执行在LINQ拉姆达多个表之间的加入 [英] How to perform Join between multiple tables in LINQ lambda

查看:131
本文介绍了如何执行在LINQ拉姆达多个表之间的加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图执行在LINQ多个表之间加入。我有以下类:

I am trying to perform a Join between multiple tables in LINQ. I have the following classes:

Product {Id, ProdName, ProdQty}

Category {Id, CatName}

ProductCategory{ProdId, CatId} //association table

和我用下面的code(其中产品类别产品分类是上面的类的实例):

And I use the following code (where product, category and productcategory are instances of the above classes):

var query = product.Join(productcategory, p => p.Id, pc => pc.ProdID, (p, pc) => new {product = p, productcategory = pc})
                   .Join(category, ppc => ppc.productcategory.CatId, c => c.Id, (ppc, c) => new { productproductcategory = ppc, category = c});

有了这个code我从下面的类的对象:

With this code I obtain an object from the following class:

QueryClass { productproductcategory, category}

在哪里producproductcategory的类型是:

Where producproductcategory is of type:

ProductProductCategoryClass {product, productcategory}

我不明白的地方联接的表是,我期待的单班包含从参与类的所有属性。

I do not understand where the joined "table" is, I was expecting a single class that contains all the properties from the involved classes.

我的目的是来用查询产生的一些属性另一个对象:

My aim is to populate another object with some properties resulting from the query:

CategorizedProducts catProducts = query.Select(m => new { m.ProdId = ???, m.CatId = ???, //other assignments });

我怎样才能实现这一目标?

how can I achieve this goal?

推荐答案

有关加入,我强烈preFER查询语法被愉快地隐藏所有细节(而不是其中最重要的是参与了透明标识符沿途中间的预测是在点语法相当于明显)。但是,你问关于lambda表达式,我觉得你有你需要的一切 - 你只需把它放在一起

For joins, I strongly prefer query-syntax for all the details that are happily hidden (not the least of which are the transparent identifiers involved with the intermediate projections along the way that are apparent in the dot-syntax equivalent). However, you asked regarding Lambdas which I think you have everything you need - you just need to put it all together.

var categorizedProducts = product
    .Join(productcategory, p => p.Id, pc => pc.ProdId, (p, pc) => new { p, pc })
    .Join(category, ppc => ppc.pc.CatId, c => c.Id, (ppc, c) => new { ppc, c })
    .Select(m => new { 
        ProdId = m.ppc.p.Id, // or m.ppc.pc.ProdId
        CatId = m.c.CatId
        // other assignments
    });

如果你需要,可以保存连接成一个局部变量和以后重新使用它,但缺乏其他细节,相反,我认为没有理由引进局部变量。

If you need to, you can save the join into a local variable and reuse it later, however lacking other details to the contrary, I see no reason to introduce the local variable.

此外,您可以扔选择进入第二加入(同​​样,只要最后的lambda有不依赖于联接结)这将使其他操作:

Also, you could throw the Select into the last lambda of the second Join (again, provided there are no other operations that depend on the join results) which would give:

var categorizedProducts = product
    .Join(productcategory, p => p.Id, pc => pc.ProdId, (p, pc) => new { p, pc })
    .Join(category, ppc => ppc.pc.CatId, c => c.Id, (ppc, c) => new {
        ProdId = ppc.p.Id, // or ppc.pc.ProdId
        CatId = c.CatId
        // other assignments
    });

...并作出最后一次尝试向你推销的查询语法,这应该是这样的:

...and making a last attempt to sell you on query syntax, this would look like this:

var categorizedProducts =
    from p in product
    join pc in productcategory on p.Id equals pc.ProdId
    join c in category on pc.CatId equals c.Id
    select new {
        ProdId = p.Id, // or pc.ProdId
        CatId = c.CatId
        // other assignments
    };

您的手可以在查询语法是否可被捆绑。我知道有些店有这样的任务 - 通常是基于概念,即查询语法一定程度上比点语法较为有限。还有其他原因,比如我为什么要学习第二语法,如果我可以做点语法一切吗?由于这最后一部分显示 - 有细节,查询语法皮,可以使其非常值得的提高可读性,它拥抱带来的:所有的中间预测和识别你做饭行动是不愉快前端和中心 - 现阶段在查询语法的版本 - 他们是背景绒毛。关闭我的香皂盒了 - 无论如何,感谢您的问题。 :)

Your hands may be tied on whether query-syntax is available. I know some shops have such mandates - often based on the notion that query-syntax is somewhat more limited than dot-syntax. There are other reasons, like "why should I learn a second syntax if I can do everything and more in dot-syntax?" As this last part shows - there are details that query-syntax hides that can make it well worth embracing with the improvement to readability it brings: all those intermediate projections and identifiers you have to cook-up are happily not front-and-center-stage in the query-syntax version - they are background fluff. Off my soap-box now - anyhow, thanks for the question. :)

这篇关于如何执行在LINQ拉姆达多个表之间的加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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