简体   繁体   English

select 首先不是 null 在 dot.net 实体代码中分组时每个组中的字段

[英]how to select first not null field in each group when group by in dot net entity code first

I have a Model with a field type of string .我有一个field类型为string的 Model 。

I want to make a query in group by get first item of each group .我想group by获取每个groupfirst项来在组中进行查询。

but with this condition that first item string field is not null但在这种情况下,第一项string field not null

i run this query in my code but got error我在我的代码中运行此查询但出现错误

var items = appDbContext.Foods.GroupBy(r => r.Category)
    .Select(g => new { g.Key,image= g.Where(r=>!string.IsNullOrEmpty(r.Image)).First().Image??"" })
    .ToDictionary(k => k.Key, k => k.image);

I got below error:我收到以下错误:

System.InvalidOperationException
  HResult=0x80131509
  Message=The LINQ expression 'GroupByShaperExpression:
KeySelector: f.Category, 
ElementSelector:EntityShaperExpression: 
    EntityType: Foods
    ValueBufferExpression: 
        ProjectionBindingExpression: EmptyProjectionMember
    IsNullable: False

    .Where(r => !(string.IsNullOrEmpty(r.Image)))
    .Select(s => s.Image)
    .First()' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

My C# model:我的C# model:

    public class Foods
    {
        public int FoodId { get; set; }
        public string Name { get; set; }
        public int Price { get; set; }
        public string Image { get; set; }
        public string Category { get; set; }
        public string Description { get; set; }
        public DateTimeOffset InsertDate { get; set; }
        public bool IsActive { get; set; }
    }

Assuming that you want to hide the categories without images, you could do it like this:假设你想隐藏没有图像的类别,你可以这样做:

foods.Where(x => !string.IsNullOrEmpty(x.Image))
    .GroupBy(x => x.Category)
    .Select(x => x.First());

Basically, you can filter out all the items without images and then use the group by.基本上,您可以过滤掉所有没有图像的项目,然后使用分组依据。 However, if a category(group) has all the items without images, it will be skipped.但是,如果一个类别(组)的所有项目都没有图像,它将被跳过。

If you want to have the null categories also, this works for me (note that this was tested with in-memory collections, not with EF)如果你也想拥有 null 类别,这对我有用(请注意,这是用内存中的 collections 测试的,而不是 EF)

var foods = new List<Foods>()
{
    new Foods()
    {
        FoodId = 1,
        Name = "Test1",
        Price = 20,
        Image = "my-image",
        Category = "category1",
        Description = "description1",
        InsertDate = DateTimeOffset.Now,
        IsActive = true
    },
    new Foods()
    {
        FoodId = 1,
        Name = "Test2",
        Price = 20,
        Image = "my-image",
        Category = "category1",
        Description = "description1",
        InsertDate = DateTimeOffset.Now,
        IsActive = true
    },
    new Foods()
    {
        FoodId = 1,
        Name = "Test3",
        Price = 20,
        Category = "category2",
        Description = "description1",
        InsertDate = DateTimeOffset.Now,
        IsActive = true
    },
    new Foods()
    {
        FoodId = 1,
        Name = "Test4",
        Price = 20,
        Category = "category2",
        Description = "description1",
        InsertDate = DateTimeOffset.Now,
        IsActive = true
    }
};

var filteredFoods = foods
    .GroupBy(x => x.Category)
    .Select(x => x.FirstOrDefault( t=> !string.IsNullOrEmpty(t.Image)) ?? x.First());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM