Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Processing of the LINQ expression 'GroupByShaperExpression" Got error after upgrade to ef core . 5

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.

like image 497
sunny Avatar asked Oct 27 '25 19:10

sunny


1 Answers

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(); 
like image 167
Svyatoslav Danyliv Avatar answered Oct 29 '25 08:10

Svyatoslav Danyliv