locked
LINQ to Entities does not recognize the method 'System.String ToString()' method RRS feed

  • Question

  • User1622294484 posted

    Hi all,

    I want to order a query by "status" , this status is an enumerations that is stored in the db as an int, but I get an exception thrown :

    LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression

                                query = query.OrderByDescending(d => ((Enumerations.StatutProjet)d.Status).ToString());

    Any idea how to solve this problem ?

    Thursday, July 2, 2015 4:35 AM

Answers

  • User-271186128 posted

    Hi kryptong

    As for this issue, I suggest you could refer to the following code:

    Code in SQL:

    create table UserTable
    (
    ID int primary key identity(1001,1),
    Name varchar(30),
    [Status] int,
    )
    go
    
    insert into UserTable
    select 'AAA',1 union
    select 'BBB',2 union
    select 'CCC',3 union
    select 'DDD',4 union
    select 'EEE',1 union
    select 'FFF',2 union
    select 'GGG',3 union
    select 'HHH',4 union
    select 'III',1 union
    select 'JJJ',2 
    GO

    Code in Page

            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write((StatutProjet)1);
                using (Entities context = new Entities())
                {
                    var query = context.UserTables.OrderByDescending(uu => uu.Status).Select(uu => new { ID = uu.ID, Name = uu.Name, Status = (StatutProjet)uu.Status }).ToList();
               GridView1.DataSource =query.ToList() ; GridView1.DataBind(); } }

    The output:

    ID Name Status
    
    1004 DDD OnHold 
    1008 HHH OnHold 
    1003 CCC Closed 
    1007 GGG Closed 
    1010 JJJ Offer 
    1006 FFF Offer 
    1002 BBB Offer 
    1001 AAA Active 
    1005 EEE Active 
    1009 III Active 
    

    Best Regards,
    Dillion     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 3, 2015 1:16 AM

All replies

  • User1577371250 posted

    Hi,

    try this

    query = query.OrderByDescending(d => (Enumerations.StatutProjet)d.Status.ToString());
    

    Thursday, July 2, 2015 5:35 AM
  • User1622294484 posted

    the syntax is wrong I get an error :

      Cannot convert type 'string' to 'MyProject.Objects.Enumerations.StatutProjet'  

    Thursday, July 2, 2015 5:51 AM
  • User1577371250 posted
    query = query.OrderByDescending(d => d.Status.ToString());
    Thursday, July 2, 2015 5:55 AM
  • User1622294484 posted

    Now this will not display the correct result,because I have an enumeration as following

            public enum StatutProjet : int
            {
                [StringDescriptionAttribute("Active")]
                Active=1,
                [StringDescriptionAttribute("Offer")]
                Offer,
                [StringDescriptionAttribute("Closed")]
                Closed,
                [StringDescriptionAttribute("On Hold")]
                OnHold
            }

    if I order as you said, it will order the integer and not the string description

    Thursday, July 2, 2015 6:00 AM
  • User-271186128 posted

    Hi kryptong

    As for this issue, I suggest you could refer to the following code:

    Code in SQL:

    create table UserTable
    (
    ID int primary key identity(1001,1),
    Name varchar(30),
    [Status] int,
    )
    go
    
    insert into UserTable
    select 'AAA',1 union
    select 'BBB',2 union
    select 'CCC',3 union
    select 'DDD',4 union
    select 'EEE',1 union
    select 'FFF',2 union
    select 'GGG',3 union
    select 'HHH',4 union
    select 'III',1 union
    select 'JJJ',2 
    GO

    Code in Page

            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write((StatutProjet)1);
                using (Entities context = new Entities())
                {
                    var query = context.UserTables.OrderByDescending(uu => uu.Status).Select(uu => new { ID = uu.ID, Name = uu.Name, Status = (StatutProjet)uu.Status }).ToList();
               GridView1.DataSource =query.ToList() ; GridView1.DataBind(); } }

    The output:

    ID Name Status
    
    1004 DDD OnHold 
    1008 HHH OnHold 
    1003 CCC Closed 
    1007 GGG Closed 
    1010 JJJ Offer 
    1006 FFF Offer 
    1002 BBB Offer 
    1001 AAA Active 
    1005 EEE Active 
    1009 III Active 
    

    Best Regards,
    Dillion     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 3, 2015 1:16 AM