如何选择公司名称和产品编号?

How to select CompanyName and number of its products?
2021-07-22
  •  译文(汉语)
  •  原文(英语)

我想写一个LINQ查询将返回,同列CompanyNameSupplier和本公司所有产品的数量.你能帮助我吗?

到目前为止,我得到了:

var company = from pro in db.Products
              join sup in db.Suppliers
                  on pro.SupplierID equals sup.SupplierID
              group pro by pro.SupplierID 
              into g
              select new { Name = g.Key, COUNT = g.Count() };

但这SupplierID不会返回CompanyName.数据库是Northwnd.

速聊1:
您不确定什么?您不知道此特定查询的作用,因此不确定如何更改它吗?
速聊2:
它返回SupplierID,并且我想要来自Supplier的CompanyName
速聊3:
我了解这部分,但是我只是想弄清楚您是否了解整个查询的实际作用.
速聊4:
group pro by pro.SupplierID 对我来说还不清楚.
解决过程1

使用组联接(即join ... into)将产品与供应商联接,并在组中获得供应商的所有产品:

from s in db.Suppliers
join p in db.Products
   on s.SupplierID equals p.SupplierID into g
select new {
   s.CompanyName,
   ProductsCount = g.Count()
}
速聊1:
这甚至更好:)
解决过程2

以下将编译并与Linq-for-objects一起运行.我不能保证Linq-to-SQL是否会应付.

var company = from sup in db.Suppliers
              select new
              {
                  Name = sup.CompanyName,
                  COUNT = db.Products
                      .Where(pro => pro.SupplierID == sup.SupplierID)
                      .Count()
              };

I want to write a Linq query that will return, columns with CompanyName of Supplier and number of all products of this company. Can you help me?

So far I got this:

var company = from pro in db.Products
              join sup in db.Suppliers
                  on pro.SupplierID equals sup.SupplierID
              group pro by pro.SupplierID 
              into g
              select new { Name = g.Key, COUNT = g.Count() };

But this returns SupplierID not CompanyName. Database is Northwnd.

Talk1:
What are you not sure about? Do you not know what this specific query does, so you aren't sure how to change it?
Talk2:
It returns SupplierID, and I want CompanyName from Supplier
Talk3:
I understand that part, but I am just trying to figure out if you understand what the entire query is actually doing.
Talk4:
group pro by pro.SupplierID is a bit unclear for me.
Solutions1

Use group join (i.e. join...into) to join suppliers with products and get all products of supplier in group:

from s in db.Suppliers
join p in db.Products
   on s.SupplierID equals p.SupplierID into g
select new {
   s.CompanyName,
   ProductsCount = g.Count()
}
Talk1:
this is even better :)
Solutions2

The following compiles and runs with Linq-for-objects. I can't vouch for whether Linq-to-SQL will cope.

var company = from sup in db.Suppliers
              select new
              {
                  Name = sup.CompanyName,
                  COUNT = db.Products
                      .Where(pro => pro.SupplierID == sup.SupplierID)
                      .Count()
              };
转载于:https://stackoverflow.com/questions/17378329/how-to-select-companyname-and-number-of-its-products

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入