【发布时间】:2021-03-31 20:58:08
【问题描述】:
我的项目是 .net core 2.2 并升级到 .net 5 和 EF 5.0 我的最后一个查询是
public IList<Group<String, UsefullLink>> GetAllGroupActive()
{
return (from b in _db.UsefullLinks
group b by b.UseFullLinkType.Name into g
select new Univer.Business.Model.Group<string, UsefullLink>
{
Key = g.Key,
Values = g
}).ToList();
}
我的模特
public class UsefullLink
{
public int Id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }
public DateTime CreateDate { get; set; }
public string Image { get; set; }
public int LinkTypeId { get; set; }
public LinkType LinkType { get; set; }
public int Priority { get; set; }
...
public string Description { get; set; }
public int UseFullLinkTypeId { get; set; }
public UseFullLinkType UseFullLinkType { get; set; }
}
public class UseFullLinkType
{
public int Id { get; set; }
[Required, DataType(DataType.Text)]
public string Name { get; set; }
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[Required, DataType(DataType.Date)]
public DateTime InputDate { get; set; }
public int Priority { get; set; }
public ICollection<UsefullLink> UsefullLinks { get; set; }
}
我的看法
@using Univer.Business.Model
@model List<Group<string, Univer.Model.UsefullLink>>
@{
ViewData["Title"] = "ServiceTebbedTable";
}
<div class="container">
<div class="row UsefullLink ">
<div class="card ">
<div class="card-header"> <a asp-action="ServiseTable" asp-controller="Home" asp-area="">service table</a></div>
<ul class="row" style="margin-left:25px ;margin-right:25px;">
@foreach (var group in Model)
{
<li class="row col-md-12" id="cut-diamond"> @group.Key</li>
<li class="m-1"></li>
foreach (var link in group.Values)
{
<li class="col-md-4 mt-2 justify-content-center">
@if (link.Image != "1")
{
<img src="@Url.Content(link.Image)" class="UsefullLink-icon" />
}
else
{
<img src="~/images/Logo200blue.png" class="UsefullLink-icon" />
}
<a href="@link.LinkUrl" class="justify-content-center" target="_blank"> @link.Name</a>
</li>
}
}
</ul>
</div>
</div>
</div>
运行项目后出现此错误
InvalidOperationException:处理 LINQ 表达式“GroupByShaperExpression: KeySelector: u.Name, 元素选择器:实体形状表达式: 实体类型:UsefullLink 值缓冲区表达式: ProjectionBindingExpression:EmptyProjectionMember IsNullable: 假
更新1: 对于组,我在 Univer.Business.Model 中有这个
public class Group<k, T>
{
public k Key;
public IEnumerable<T> Values;
}
更新2 这是我的存储库的构造函数 公共类 UsefullLinkRepository : IUsefullLinkRepository { 私有只读UniverContext _db;
public UsefullLinkRepository(UniverContext db)
{
_db = db;
}
}
我将它的方法更改为
var data = from b in _db.UsefullLinks.AsEnumerable()
join c in _db.UseFullLinkTypes on b.UseFullLinkTypeId equals c.Id
group b by b.UseFullLinkType.Name into g
select g;
return data.Select(g => new Group<string, UsefullLink>
{
Key = g.Key,
Values = g
}).ToList();
运行后Update2 我收到了这个错误
InvalidOperationException: 已经有一个打开的 DataReader 与此 Connection 关联,必须先关闭。
【问题讨论】:
-
我建议将 Where 移近 ToListAsync。它大大简化了查询翻译。
-
你能分享一下你的模型的详细代码吗?你的 UsefullLink 和 UseFullLinkType 是什么?
标签: c# entity-framework linq asp.net-core