【问题标题】:Northwind Database Linq Query罗斯文数据库 Linq 查询
【发布时间】:2016-12-27 11:24:30
【问题描述】:

我是 Linq 的新手,我正在尝试使用 northwind 数据库编写一个查询,该查询应该返回所有在同一类别中拥有两种或更多产品的供应商。

var test1 =
(from p in Products
join pi in Products on p.CategoryID equals pi.CategoryID
join pf in Products on p.SupplierID equals pf.SupplierID
where p.ProductID != pi.ProductID
select new 
{p.ProductName, p.CategoryID, p.SupplierID}).ToList().Distinct();


test1.Dump();

这是我最后一次失败的尝试。我有点听天由命了,因为我已经尝试了几个小时来解决这个问题,但它仍然不会做它应该做的事情。也许我完全弄错了?

我的方法是必须有两个或多个列表具有相同的 SupplierID 和 CategoryID 但不同的 ProductID,但我还没有找到解决方案。

【问题讨论】:

  • 你能告诉我们有什么问题吗?你期望什么,你会得到什么?

标签: c# sql linq-to-sql linqpad northwind


【解决方案1】:

最好使用 GroupBy() :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Product> Products = new List<Product>() {
                new Product() { ProductName = "ABC", CategoryID = 1, SupplierID = 1},
                new Product() { ProductName = "DEF", CategoryID = 1, SupplierID = 1},
                new Product() { ProductName = "GHI", CategoryID = 1, SupplierID = 3},
                new Product() { ProductName = "JKL", CategoryID = 1, SupplierID = 3},
                new Product() { ProductName = "MNO", CategoryID = 2, SupplierID = 1},
                new Product() { ProductName = "PQR", CategoryID = 3, SupplierID = 1},
                new Product() { ProductName = "STU", CategoryID = 4, SupplierID = 1},
                new Product() { ProductName = "VWX", CategoryID = 4, SupplierID = 1},
                new Product() { ProductName = "YZ1", CategoryID = 4, SupplierID = 1},
                new Product() { ProductName = "234", CategoryID = 5, SupplierID = 1}
            };



            var test1 = Products.GroupBy(x => new { supplier = x.SupplierID, category = x.CategoryID })
                .Where(x => x.Count() >= 2).Select(y => y.Select(z => new { name = z.ProductName, supplier = y.Key.supplier, category = y.Key.category })).SelectMany(x => x).ToList();

            foreach (var item in test1)
            {
                Console.WriteLine("Name = '{0}', Supplier = '{1}', Category = '{2}'", item.name, item.supplier, item.category);
            }
            Console.ReadLine();
        }

    }
    public class Product
    {
        public string ProductName { get; set; }
        public int CategoryID { get; set; }
        public int SupplierID { get; set; }
    }
}

【讨论】:

    【解决方案2】:

    缺少你想要的结果,但我可以告诉你,你现在所做的不会有什么好处。

    简单地说,您当前要求数据库返回与所有产品匹配的所有产品的所有产品,如果数据库没有超时,这基本上会导致您获得所有产品。因此,我们可以将您的查询简化为:

    var test1 =
    (from p in Products
    select new 
    {p.ProductName, p.CategoryID, p.SupplierID}).ToList().Distinct();
    

    然后,您希望从该选择中选择产品名称、类别和供应商的唯一列表。这里的主要问题是:您想要一个唯一的组合列表,还是三个属性之一需要是唯一的?假设第一个,获得结果的最简单方法是:

    public class ProductResult  : IEquatable<ProductResult> // we have to tell C# compiler how to check if the objects are different from one another
    {
       public string Name { get; set; }
       public string Category { get; set; }
       public string Supplier { get; set; }
    
       public bool Equals(ProductResultother)
                {
                    if (other == null)
                        return false;
    
                    return (Category == other.Category) 
                           && Supplier  == other.Supplier)
                           && Name  == other.Name ); // optionally do an .Equals() to make this case-insensitive (check for nulls first if you do this!)
                }
    }
    

    那么你可以这样做:

    var test1 = (from p in Products
    select new ProductResult()
    {
       Name = p.ProductName,
       Category = p.CategoryId,
       Supplier = p.SupplierID,
    }).Distinct();
    

    您现在拥有所有唯一产品名称/类别/供应商组合的列表。

    【讨论】:

      猜你喜欢
      • 2012-04-22
      • 2011-01-23
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      相关资源
      最近更新 更多