C
C#2y ago
kopuo

❔ Calling extensions method in linq query

Hi, is there any way to use my own extension method in a linq to query? With the approach below, it throws me an exception about the method not being able to be translated to SQL. If I add ..AsEnumerable().Select(...).AsQueryable() will it be a correct solution?
var productsQuery = _dbContext.Products.AsNoTracking().Select(p => new ProductModel
{
Name = p.Name,
Quantity = p.Quantity.MyExtensionMethod(), // p.Quantity == null ? "None" : p.Quantity.ToString()
Price = p.Price
});
var productsQuery = _dbContext.Products.AsNoTracking().Select(p => new ProductModel
{
Name = p.Name,
Quantity = p.Quantity.MyExtensionMethod(), // p.Quantity == null ? "None" : p.Quantity.ToString()
Price = p.Price
});
6 Replies
Denis
Denis2y ago
Stack Overflow
Workarounds for using custom methods/extension methods in LINQ to E...
I have defined a GenericRepository class which does the db interaction.
protected GenericRepository rep = new GenericRepository(); And in my BLL classes, I can query the db like:
public Li...
Denis
Denis2y ago
this is what I found
Denis
Denis2y ago
Engineering Education (EngEd) Program | Section
Adding Custom Methods to LINQ Queries in C#
This article will explain how to add custom methods to LINQ queries and how to customize LINQ queries. We will develop an application that retrieves JSON data from an API, query the data using the LINQ queries.
Denis
Denis2y ago
Packt
Extension methods on IQueryable (Become an expert) | Instant .NET 4...
IQueryable is used to operate mainly on databases. IQueryable are an extension from IEnumerable, hence, we can call all extensions and methods of IEnumera
Anton
Anton2y ago
no, converting to IEnumerable will evaluate that on the client. in your case it doesn't matter but in general, don't do that
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server