【问题标题】:Join on list of List in C#加入 C# 中的列表列表
【发布时间】:2016-05-20 05:32:55
【问题描述】:

我没有尝试使用join,但是今天我想使用join来解决一个复杂的情况,我有一个数据在下面的示例中列出。我也发表了评论。下一步怎么走?

namespace JoinsApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Category> categories = new List<Category>()
            { 
                new Category(){Name="Beverages", ID=001,Products =new List<Product>(){new Product{Name="Cola",  CategoryID=001},new Product{Name="Tea",  CategoryID=001} } },
                new Category(){ Name="Condiments", ID=002,Products =new List<Product>(){new Product{Name="Mustard",  CategoryID=002},new Product{Name="Pickles",  CategoryID=003} } },
                new Category(){ Name="Vegetables", ID=003,Products =new List<Product>(){new Product{Name="Carrots",  CategoryID=003},new Product{Name="Melons",  CategoryID=003} } },
                new Category() {  Name="Grains", ID=004,Products =new List<Product>(){new Product{Name="Carrots",  CategoryID=003},new Product{Name="Melons",  CategoryID=003} } },
                new Category() {  Name="Fruit", ID=005,Products =new List<Product>(){new Product{Name="Peaches",  CategoryID=005},new Product{Name="Melons",  CategoryID=005} } }      
            };

            //filter data using join, i want to filter data and want a  condition on cat.ID and Product.CategoryID, but Products property is not accessible using category alias.  
            //var u= from cat in categories join product in cat.Products on product.CategoryID equels cat.ID select cat.Name, product.Name;
        }
    }

    #region Join Demonstration data

    public class Product
    {
        public string Name { get; set; }
        public int CategoryID { get; set; }
    }

    public class Category
    {
        public string Name { get; set; }
        public int ID { get; set; }
        public List<Product> Products { get; set; }
    }

    // Specify the first data source.


    #endregion

}

【问题讨论】:

  • 你能举一个预期输出的例子吗?

标签: c# sql join


【解决方案1】:

简单。您只需要完整的产品列表即可完成此操作,这可以使用SelectMany 来实现:

var uu = (from cat in categories
            join product in categories.SelectMany(p => p.Products)
            on cat.ID equals product.CategoryID
            select new
            {
                CategoryName = cat.Name,
                ProductName = product.Name
            }).ToList();

编辑(使用 Lambda):

var uuLambda = categories.Join(categories.SelectMany(p => p.Products),
                                cat => cat.ID, prod => prod.CategoryID,
                                (cat, prod) => new { CategoryName = cat.Name, ProductName = prod.Name }).ToList();

以下是基于您的评论,无论如何这不是一个好主意,而且不可读:

var uuWhere = categories.SelectMany(p => p.Products).
                         Select(prod => new { ProductName = prod.Name,
                                              CategoryName = categories.Where(x => x.ID == prod.CategoryID).
                                                             Select(x => x.Name).FirstOrDefault() }).ToList();

【讨论】:

  • 谢谢,解决方案符合我的预期。
  • 如何使用 lambda 表达式?如果您有任何想法,请回复。
  • 你的意思是没有“from in”语法?
  • 类似这样的东西:- var lamda = categories.Where(c => { return c.ID == 001;//CategoryID });
  • 再次感谢。但我需要使用 where 子句。
猜你喜欢
  • 1970-01-01
  • 2013-12-31
  • 2017-02-07
  • 2015-10-07
  • 2021-06-17
  • 2011-05-31
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
相关资源
最近更新 更多