giovanna zoppi
giovanna zoppi

Reputation: 91

How to create a new DateTime where condition using table field with LINQ

i have a win application in c# where i would obtain a linq query with a where condition containing a Data concatened by table fields. my query is :

var fatture = (from t016 in cont.T016_FATT
              where t016.ID_FATTURA = aiIdFattura
              && Convert.toDateTime("01" + t016.COD_MESE+"/"+t016.ANNO) > aiData
              select t016).ToList();

where t016.COD_MESE is a string rapresenting month value, aiIdFattura is a decimal parameter passed in function and aiData is a DateTime parameter.

but this linq query gives me this error:

LINQ to Entities does not recognize the method Convert.ToDateTime

i have found in internet a solution suggesting to use DBFunctions that is present in EntityFramework (version 6) but i have a minor version of EntityFramework so i cannot use this function.

another found solution was to format date before linq query but in my case is not possible because depending from T016_FATT so i cannot format my date first.

so have you any idea to suggest me?

thanks a lot

Upvotes: 2

Views: 824

Answers (1)

ocuenca
ocuenca

Reputation: 39346

Remember your query is going to be translated to SQL, and in this case your Linq provider doesn't know how to translate that method call to a sql statement. To avoid that issue, you can make the switch of Linq to Entities to Linq to Objects using AsEnumerable extension method. After you call that method you can apply your date conversion:

var fatture = cont.T016_FATT.Where(e=>e.ID_FATTURA == aiIdFattura)
                            .AsEnumerable()
                            .Where(e=>Convert.toDateTime("01" +"/"+ e.COD_MESE+"/"+e.ANNO) > aiData)
                            .ToList();

If you want to see the methods that are supported by Linq to Entities, you can check this link.

Upvotes: 1

Related Questions