none
Convert to Int Problem RRS feed

  • Question

  • Hi everyone, 

    I have some issue with the Convert.ToInt32 / Parse.Int, my column at DB is varchar type and I need to compare with an Int32 Type with >= operator. Here's some piece of the code I'm using, Hope you can help me.

    var municipio = this.MunicipioRepository.Find(x => ((Int32.Parse(cep) >= Int32.Parse(x.CEPInicial)))
                                    &&
                                    ((Int32.Parse(cep) <= Int32.Parse(x.CEPFinal))));

    Thank you so much.

    Felippe Rodrigues.


    • Edited by FRodrighues Tuesday, January 13, 2015 6:49 PM
    Tuesday, January 13, 2015 6:40 PM

Answers

  • Hello,

    >>I have some issue with the Convert.ToInt32 / Parse.Int,

    Since you posted this issue to this forum, I assume that you are write an IQueryable query. Then in LINQ2Entites, methods as Convert.ToInt32 / Parse.Int are not supported by default. One way is to use LINQ2Object, you could load data to your local and then you could use these CLR methods:

    using (DFDBEntities db = new DFDBEntities())
    
                    {
    
                        var result = db.Orders.ToList().Where(o => Int16.Parse(o.TestName) >= 1).ToList();
    
                    }
    

    This is proper for scenario that stored data is not large.

    Another way is to write your EDMfunction and here I made an example:

    Write EDM function in you context class:

    [EdmFunction("DFDBModel", "ConvertToIntFromString")]
    
            public static int ConvertToIntFromString(string TestName)
    
            {
    
                throw new NotSupportedException("Direct calls are not supported.");
    
            }
    

    Write the cast in the demx(open it with notepad) and add below code to schema section:

    <Function Name="ConvertToIntFromString" ReturnType="Edm.Int32">
    
              <Parameter Name="TestName" Type="Edm.String" />
    
              <DefiningExpression>
    
                Cast(TestName as Edm.Int32)
    
              </DefiningExpression>
    
            </Function>
    

    Calling this method as:

    using (DFDBEntities db = new DFDBEntities())
    
                    {
    
                        var result = db.Orders.Where(o => DFDBEntities.ConvertToIntFromString(o.TestName) > 1).ToList();
    
                    }
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by PhGr_ Wednesday, January 14, 2015 10:01 AM
    • Marked as answer by Fred BaoModerator Friday, January 23, 2015 9:41 AM
    Wednesday, January 14, 2015 9:31 AM
    Moderator