Linq查询与聚合

时间:2009-02-25 15:07:57

标签: c# linq linq-to-objects

我想编写一个优雅的linq查询来处理以下SAMPLE对象模型:

    class Category
    {
        public string Name { get; set; }        
        public IList<Product> Products { get; set;}        
    }

    class Product
    {
        public string Title { get; set; }
        public IList<Photo> Photos { get; set; }
    }

    class Photo
    {
        public int Id { get; set; }
    }

我构造了以下查询来获取Photo Id:

    var query = from category in Factory.GetCategories()
                where category.Name == "Cameras"
                select (from product in category.Products
                        where product.Title == "Sony"
                        select (from photo in product.Photos
                                select photo.Id)
                               );

    var v = query.ToList();

当查询没有正确投影时,我必须为每个子选择添加FirstOrDefault()!:

var query = from category in Factory.GetCategories()
                where category.Name == "Cameras"
                select (from product in category.Products
                        where product.Title == "Sony"
                        select (from photo in product.Photos
                                select photo.Id).FirstOrDefault()
                               ).FirstOrDefault();

var v = query.ToList();

有更好的方法吗?忽略我们没有处理数据库和PK / FK的事实并没有发挥作用。

当我在linq查询中做同样的事情时,我真的想避免编写一个大的for循环:

foreach (var category in Factory.GetCategories())
            {
                if (category.Name == "Camera")
                {
                    foreach (var product in category.Products)
                    {
                        if (product.Title == "Sony")
                        {
                            foreach (var photo in product.Photos)
                            {
                                //get data
                                int id = photo.Id;
                            }
                        }
                    }
                }                
            }

实际实现比这个简单的对象模型更复杂。我想从这个简单的样本中得到基本的想法,所以我将它应用到我的真实对象模型中。

干杯!

4 个答案:

答案 0 :(得分:5)

你只想要扁平的ids吗?

        var query = from category in Factory.GetCategories()
                    where category.Name == "Cameras"
                    from product in category.Products
                    where product.Title == "Sony"
                    from photo in product.Photos
                    select photo.Id;

答案 1 :(得分:2)

是的 - 您必须添加FirstOrDefault,因为没有“照片ID”这样的东西。每张相机可能有很多照片 - 您想要哪种?

如果您只关心第一张照片,那很好 - 虽然多个“来自”条款会让您的生活更轻松:

var query = from category in Factory.GetCategories()
            where category.Name == "Cameras"
            from product in category.Products
            where product.Title == "Sony"
            select product.Photos.Select(photo => photo.Id)
                                 .FirstOrDefault();

请注意,对于没有照片的产品,它将返回0。这就是你想要的吗?

如果您的要求更清楚,我们会更好地帮助您。

编辑:如果您只想要任何 Sony相机的第一个ID,请使用:

var query = from category in Factory.GetCategories()
            where category.Name == "Cameras"
            from product in category.Products
            where product.Title == "Sony"
            from photo in product.Photos
            select photo.Id;
var firstId = query.FirstOrDefault();

答案 2 :(得分:1)

与其他人一样的答案,但使用扩展方法调用而不是查询理解。 SelectMany允许您“解包”子集合并继续在该级别进行查询。

var query = Factory.GetCategories()
  .Where(category => category.Name == "Cameras")
  .SelectMany(category => category.Products)
  .Where(product => product.Title == "Sony")
  .SelectMany(product => product.Photos)
  .Select(photo => photo.Id);

答案 3 :(得分:0)

        var ids = from category in Factory.Categories
                  where category.Name == "Cameras"
                  from product in category.Products
                  where product.Title == "Sony"
                  from photo in product.Photos
                  select photo.Id;

返回照片的所有ID,无照片时可枚举为空。 如果你想要第一个,或默认为0:

var result = ids.FirstOrDefault();