Property with GET (Getteronly) with Iqueryable C#

Asked

Viewed 48 times

0

I don’t know exactly what it’s called when it maps the return in the method like this, but I’ll try to explain, I have the following class for example:

public class Venda(){
   public decimal Valor {get;set;}
   public decimal Desconto {get;set;}
   public decimal ValorLiquido => Valor - Descontos;
}

I use Entityframework and this class would be a model of the Database, I need to make a sales report and in this report I use the Property Value Net and the problem this here I do the following query:

var vendas = ctx.Vendas;

This returns me a Iqueryable so far ok, but if I try to use the property Valor liquido returns me the following message:

The specified type Member 'Valorliquido' is not supported in LINQ to Entities. Only initializers, Entity Members, and Entity navigation properties are supported.

If I give ToList() it works and I can access the property, only it’s a report with more than 50,000 items, and in the real class, there are about 10 Getteronly Property for more, so it’s impossible to use ToList(), The question is: Would have some way to access this property without giving Tolist() in the case in a Iqueryable list?

  • Why not use a View that loads your already calculated data, so much lower output (computes on the server), instead of loading 50K of records...and when it reaches 10 million? What you do?

  • Would it be possible to parameterize this view by the application? In the case of this report the User can choose the filters, as date, form of payment and etc

  • Views are like a normal query to a table, but it allows you to map your output (only return what you want to see/access), make joins with other tables, calculations, aggregate data etc, doc microsoft https://docs.microsoft.com/en-us/sql/statements/create-view-Transact-sql?view=sql-server-2017. Its use in SQL is how to select a table to type SELECT * FROM [View_name] WHERE User LIKE '%An%', its use in EF, you have a post here https://stackoverflow.com/questions/7461265/how-use-views-in-code-first -Entity-framework.

  • a view loads all the data before making the filters, will give you a headache, if you want to decrease the result set use a Where in your Linq ex: . Where(x=>x. liquid value=10). Tolist()

  • 2

    @Edenilsonbila from a read on this: https://answall.com/questions/17284/qual-a-diferen%C3%A7a-entre-ienumerable-iqueryable-e-list Iqueryable is a query tree, the data has not been "materialized" yet. so it is not possible to access the property Valorliquido

1 answer

0

Have you tried to ignore the mapping of this field in Entityframework by adding the 'Notmapped attribute' ?

Example:

public class Venda(){
   public decimal Valor {get;set;}
   public decimal Desconto {get;set;}
   [NotMapped]
   public decimal ValorLiquido => Valor - Descontos;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.