Unable to perform Group join in linq in .net6 [duplicate] - linq

This question already has answers here:
The LINQ expression 'Expression' could not be translated. Either rewrite the query in a form that can be translated
(1 answer)
Query with `groupjoin` cannot be translated although it's documened as being supported
(4 answers)
Can't make GroupJoin work. NavigationExpandingExpressionVisitor exception
(1 answer)
Group join in EF Core 3.1
(1 answer)
Closed 16 days ago.
I am working on a .net6 framework where querying database through linq to sql. I tried below Group Join in linq to fetch all the Orders against Products but getting an error. Can someone help me get out of this issue?
var result =
(from pd in dataContext.tblProducts
join od in dataContext.tblOrders on pd.ProductId equals od.ProductId
into t
select new
{
ProductId = pd.ProductId,
Orders = t.ToList()
}).ToList();
Exception:
Source - Microsoft.EntityFrameworkCore
Message - The LINQ expression 'DbSet<tblProducts>()
.GroupJoin(
inner: DbSet<tblOrders>(),
outerKeySelector: pd => pd.ProductId,
innerKeySelector: od => od.ProductId,
resultSelector: (p, o) => new {
ProductId= p.ProductId,
Orders = o.ToList()
})' 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.

Related

Entity Framework Core + Count with Group By

I have a table which contains ~600k records and 33 columns. In my project I am using EF Core (2.0.1) to retrieve data from database. I am having issues with below code:
var theCounter = (from f in _context.tblData.Take(100000)
group f by f.TypeId into data
select new DataDto { ID = data.Key, Count = data.Count() }).ToList();
This code is a part of REST API and when I am testing it from SOAP UI, I am gettin timeout error. When I tested the code for
Take(1000)
There are around 300 unique TypeIds.
it works fine. Any ideas how I can make it work?
-- EDIT 1:
Here is what I see when debugging the code:
Microsoft.EntityFrameworkCore.Query:Warning: Query: '(from TblData <generated>_1 in DbSet<TblData> select [<generated>_1]).Take(__p_0)' uses a row limiting operation (Skip/Take) without OrderBy which may lead to unpredictable results.
Microsoft.EntityFrameworkCore.Query:Warning: Query: '(from TblData <generated>_1 in DbSet<TblData> select [<generated>_1]).Take(__p_0)' uses a row limiting operation (Skip/Take) without OrderBy which may lead to unpredictable results.
Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression 'GroupBy([f].TypeId, [f])' could not be translated and will be evaluated locally.
Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression 'GroupBy([f].TypeId, [f])' could not be translated and will be evaluated locally.
Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression 'Count()' could not be translated and will be evaluated locally.
Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (131ms) [Parameters=[#__p_0='?'], CommandType='Text', CommandTimeout='30']
SELECT [t2].[Id], [t2].[at], [t2].[add], [t2].[AddDate], [t2].[aftc], [t2].[aftcd], [t2].[aid], [t2].[afl], [t2].[prdid], [t2].[cid], [t2].[TypeId], [t2].[env], [t2].[ext], [t2].[extddcode], [t2].[fn], [t2].[fn], [t2].[fic], [t2].[gid], [t2].[grp], [t2].[hnm], [t2].[IP], [t2].[icid], [t2].[ln], [t2].[lg], [t2].[pcid], [t2].[ret], [t2].[rts], [t2].[rnam], [t2].[sled], [t2].[seq], [t2].[sid], [t2].[styp]
FROM (
SELECT TOP(#__p_0) [t1].[Id], [t1].[at], [t1].[add], [t1].[AddDate], [t1].[aftc], [t1].[aftcd], [t1].[aid], [t1].[afl], [t1].[prdid], [t1].[cid], [t1].[TypeId], [t1].[env], [t1].[ext], [t1].[extddcode], [t1].[fn], [t1].[fn], [t1].[fic], [t1].[gid], [t1].[grp], [t1].[hnm], [t1].[IP], [t1].[icid], [t1].[ln], [t1].[lg], [t1].[pcid], [t1].[ret], [t1].[rts], [t1].[rnam], [t1].[sled], [t1].[seq], [t1].[sid], [t1].[styp]
FROM [TblData] AS [t1]
) AS [t2]
WHERE [t2].[TypeId] IS NOT NULL
ORDER BY [t2].[TypeId]
I think it is not translated properly. Any ideas why?
-- EDIT 2:
I have changed my queries to:
var query = _context.TblData
.Select(a => new {ID = a.Id, TypeId= a.TypeId})
.Distinct();
var q1 = query.GroupBy(p => p.TypeId)
.Select(g => new DataDto {TypeId= g.Key, Count = g.Count()});
return await q1.ToListAsync();
But it was translated to:
SELECT DISTINCT [a0].[Id], [a0].[TypeId] AS [TypeId]
FROM [tblData] AS [a0]
ORDER BY [a0].[TypeId]
When I checked directly in the database this query takes 14 seconds to execute. Any idea why it was not translated to something like:
SELECT DISTINCT [a0].[Id], COUNT([TypeId]) AS [TypeId]
FROM [tblData] AS [a0]
GROUP BY COUNT([a0].[Id])
ORDER BY [a0].[TypeId]
I had to upgrade EF Core version to 2.1 and LINQ is now translated properly into SQL.

LINQ to Entities does not recognize the method 'System.String PadLeft(Int32)' method [duplicate]

This question already has answers here:
LINQ to Entities does not recognize the method
(5 answers)
Closed 8 years ago.
please help me
What Should I do with this Error in this Code:
LINQ to Entities does not recognize the method 'System.String PadLeft(Int32)' method, and this method cannot be translated into a store expression.
var bookImagePage = (from bbs in LibCms.Books
join bbp in LibCms.BookPages on bbs.BookID equals bbp.BookID
select new
{
bbs.BookID,
bbp.VolumeNum,
bbs.AutolID,
bbp.PageNum,
a = bbs.AutoID.PadLeft(5, '0') + bbp.PageNo.PadLeft(4, '0')
}).Distinct().ToList();
You can't use methods in a Linq query. Take out the string manipulation then after ToList() add .Select(x=>new {
//now you can use methods on the results
})

Get index of object in a list using Linq [duplicate]

This question already has answers here:
How to get index using LINQ? [duplicate]
(7 answers)
Closed 8 years ago.
I am new to Linq. I have a Customers table.ID,FullName,Organization,Location being the columns. I have a query in Sqlite returning me 2500 records of customers. I have to find the index of the customer where ID=150 for example from this result set. Its a List of Customers. The result set of the query is ordered by organization. I tried with FindIndex and IndexOf but getting errors for the former and -1 for the latter. So, how should it be done?
Thanks.
You don't need to use LINQ, you can use FindIndex of List<T>:
int index = customers.FindIndex(c => c.ID == 150);
Linq to Objects has overloaded Select method
customers.Select((c,i) => new { Customer = c, Index = i })
.Where(x => x.Customer.ID == 150)
.Select(x => x.Index);
Keep in mind, that you should have in-memory List<Customer> to use this Linq to Objects method.

how to convert sql to linq

using ef4
iam trying to convert this sql to linq but could not
select s.IdSimcard ,s.Imei from dbo.SimCard s
where s.Imei not in (select distinct d.Imei from dbo.SimCard d inner join dbo.Configuracion c
on c.SinCard_IdSincard = d.IdSimcard
where c.Estado = 'Activo' )
So far I have this with the help of linqpad, I cant use linquer anymore for the activation code
(from s in SimCard where s.Imei.Contains( (from c in Configuracion
join d in SimCard on c.SinCard_IdSincard equals d.IdSimcard
where c.Estado == "Activo"
select new { d.Imei }).Distinct())
select new { s.IdSimcard, s.Imei })
I have read "Not in" dont supported in EF4, what woul be the equivalent?
i get this error in linqpad
'string.Contains(string)'
- Argument 1: cant convert de 'System.Linq.IQueryable' to 'string'
If I understand your question, you can use the Except(...) Extension method (in the System.Linq namespace) instead of Not Contains(...).

LINQ to NHibernate (3.0) : GroupBy and Sum in subquery gives NoTImplemented

I have a Linq query using NHibernate 3.0. But it keeps returning an error.
threw exception: System.NotImplementedException: The method or operation is not implemented..
I tried the same in LINQ 2 SQL and it works perfectly.
What might be wrong here? Here is part of my select, it's a subquery with a Groupby and Sum.
Amount = (System.Double)
((from m0 in _session.Query<Statement>()
where m0.Code== c.Code
group m0 by new
{
m0.Code
}
into g
select new
{
Expr1 = (System.Double)g.Sum(p => p.Amount)
}).First().Expr1)
};
I have the latest CSR1 installed of NHibernate but it just doesn't seem to work with my query.
The LINQ provider in NH3 is currently in a beta state. There are certain constructs that are not yet supported. (The team plans to address this after the NH3 release.) The parts causing problems in your query are the "new {}" anonymous type in the group by clause and the First() in the context of a group by. Both are not currently implemented. The following query executes properly and should give the same results:
var query = from m0 in session.Query<Statement>()
where m0.Code == c.Code
group m0 by m0.Code into g
select new {Expr1 = g.Sum(p => p.Amount)};
var result = query.ToList().First().Expr1;
First note that the "new {}" in the group by clause is not required. The other change was adding "ToList()". This forces the results to be queried from the database and then we use LINQ-to-Objects to get the First() result. The SQL generated for this query is:
select cast(sum(statement0_.Amount) as DOUBLE PRECISION) as col_0_0_
from Statement statement0_
where (statement0_.Code is null)
and ('FOO' /* #p0 */ is null)
or statement0_.Code = 'FOO' /* #p0 */
group by statement0_.Code

Resources