Comparison of = e = with strings

5

I have a field in the database that is in the following format: YYYYMM

Using procedure I can normally search these fields:

WHERE I.DT_INCL >= @inicio   
AND I.DT_INCL <= @final

When I try to pass the query to linq it returns the following error: Operator '> =' can not be applied to operands of type 'string' and 'string'

(from incorporacao in Incorporacao.FoundEntities
where incorporacao.DataInclusao >= inicio && incorporacao.DataInclusao <= final
select incorporacao).ToList();

I came up with two possible solutions:

Create a method that returns a DateTime by always setting the fixed day or leaving the field as an integer.

Which of these two solutions is the most correct in this context? The database is old and I can not modify it.

    
asked by anonymous 06.03.2014 / 13:42

3 answers

6

I have a project here with a similar problem. I use integers to store a year and month representation in the format YYYYMM without any problems. That way an integer that simulates a more recent date will always be larger than an older date.

    
06.03.2014 / 14:18
2

In stored the code will be executed by the database engine. It tries to convert String to Date .

In C # there is no such thing, so you should use data types that are comparable. I suggest you convert the Strings into DateTime to use in the LINQ expression.

    
06.03.2014 / 13:45
1

From what I understand, I believe you are looking for the most readable solution. In this case it would be to declare the format of the date and make the comparison through the method DateTime.ParseExact :

        string dateString1 = "201406";
        string dateString2 = "201407";
        string formatString = "yyyyMM";

        if (DateTime.ParseExact(dateString1, formatString, null) < DateTime.ParseExact(dateString2, formatString, null))
        {
            //Código...
        }

ps. The most performative would be the comparison of integers.

    
19.03.2014 / 13:14