如何在DOT NET ENTITY代码中进行组时,如何选择第一个组中每个组中的无效字段

发布于 2025-01-27 19:24:45 字数 1624 浏览 2 评论 0原文

我有一个带有字段类型字符串的模型。

我想通过 get 第一个 group> group 的项目进行组中的查询。

但是,对于第一个项目字符串字段而不是null

我在代码中运行此查询,但是出现错误,

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);

我的错误:

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.

我的c#模型 :

    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; }
    }

I have a Model with a field type of string.

I want to make a query in group by get first item of each group.

but with this condition that first item string field is 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 :

    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; }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回眸一遍 2025-02-03 19:24:45

假设您想在没有图像的情况下隐藏类别,则可以这样做:

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

基本上,您可以在没有图像的情况下滤除所有项目,然后使用组。但是,如果类别(组)没有图像的所有项目,则将跳过。

如果您也想拥有无效类别,这对我有用(请注意,这是通过内存收集进行测试的,而不是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());

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)

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文