Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ: Convert Guid to string

I am getting an exception when I execute this code:

var result = from menu in menuBus.GetMenus()
             orderby menu.InternalName
             select new 
             {
                 Value = menu.ID.ToString(),
                 Text = menu.InternalName
             };

var result = allMenus.ToList();

The error message says: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a stored expression.

So, I guess that something goes wrong with Value = menu.ID.ToString(). The ID property is defined as GUID (UniqueIdentifier in MS SQL).

Does anybody have a solution for this?

Thank you very much!!!

like image 285
Ingmar Avatar asked Feb 23 '23 23:02

Ingmar


1 Answers

You need to run the list through another query after querying the database.

var result = (from menu in menuBus.GetMenus()
                        orderby menu.InternalName
                        select new 
                        {
                            Value = menu.ID,
                            Text = menu.InternalName
                        }).ToList();


var result2 = (from menu in result select new {Value=menu.Value.ToString(),Text=menu.Text}).ToList();
like image 164
George Polevoy Avatar answered Mar 13 '23 01:03

George Polevoy