Reputation: 2729
My project was .net core 2.2 and upgraded to .net 5 and EF 5.0 My last query was
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();
}
My Models
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; }
}
my view
@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>
after run project I got this error
InvalidOperationException: Processing of the LINQ expression 'GroupByShaperExpression: KeySelector: u.Name, ElementSelector:EntityShaperExpression: EntityType: UsefullLink ValueBufferExpression: ProjectionBindingExpression: EmptyProjectionMember IsNullable: False
UPDATE1: and for group I have this in Univer.Business.Model
public class Group<k, T>
{
public k Key;
public IEnumerable<T> Values;
}
Update2 this is constructor for my Repository public class UsefullLinkRepository : IUsefullLinkRepository { private readonly UniverContext _db;
public UsefullLinkRepository(UniverContext db)
{
_db = db;
}
}
and I change it method to
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();
After run Update2 I got this error
InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first.
Upvotes: 4
Views: 12305
Reputation: 2729
and thanks for you help, after your help I got many Ideas, and I had many unknown think about that, another side I like Linq Lambda, I one of suggestion, I got that add "MultipleActiveResultSets=true" but I think I had not problem before upgrade my project, it mean this problem come from my query and in the next time if I want edit my another query, maybe I send many connection to database and it's not good for performance, another thinks was that I use external class from business layer of my project, it was from .net core 2.2 and in .net core 5.0 I try to use better approach, and the result was, I remove "MultipleActiveResultSets=true" from my connection string and cut use of group class from another layer of project, the final Query become in my repository :
return _db.UsefullLinks
.Join(_db.UseFullLinkTypes.AsEnumerable(),
ul => ul.UseFullLinkTypeId,
ult => ult.Id,
(ul, ult) => new { ul = ul, ult = ult }).AsEnumerable()
.GroupBy(
x=>x.ul.UseFullLinkType.Name,x=>x.ul )
.Select (g=>g).ToList();
And in my View I had small change
@model List<IGrouping<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)
{
<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>
Upvotes: 0
Reputation: 36655
Change like below:
var data = from b in _db.UsefullLinks.AsEnumerable()
join c in _db.UseFullLinkType.AsEnumerable() 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();
Result:
Upvotes: 1
Reputation: 27312
Problem here that EF 2.x silently evaluates non translatable query on the client side. It is a bug that EF Core 5 do not throw correct exception in this case.
Consider to rewrite your query
var grouped = from b in _db.UsefullLinks.AsEnumerable()
group b by b.UseFullLinkType.Name into g
select g;
return grouped.Select(g => new Univer.Business.Model.Group<string, UsefullLink>
{
Key = g.Key,
Values = g // this assignment is not translatable
}).ToList();
Upvotes: 4
Reputation: 175
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();
Here, does UsefullLinks collection has 'Key' as a column? You may want to use g.Name instead of g.Key
Upvotes: 0