如何在DOT NET ENTITY代码中进行组时,如何选择第一个组中每个组中的无效字段
我有一个带有字段
类型字符串
的模型。
我想通过 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您想在没有图像的情况下隐藏类别,则可以这样做:
基本上,您可以在没有图像的情况下滤除所有项目,然后使用组。但是,如果类别(组)没有图像的所有项目,则将跳过。
如果您也想拥有无效类别,这对我有用(请注意,这是通过内存收集进行测试的,而不是EF)
Assuming that you want to hide the categories without images, you could do it like this:
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)