0

I am simply trying to derive an array[] using the following Linq query with EFF and Mysql:

DM_ItemGroup[] array =
    (from item_grp in DbContext.hexa_item_group
     join item_btn in DbContext.hexa_button
          on item_grp.GroupID equals item_btn.ButtonID
     where item_btn.ButtonType.Equals("G", StringComparison.Ordinal)
     select new DM_ItemGroup
              {
                GroupID = SqlFunctions.StringConvert((decimal)item_grp.GroupID),
                GroupName = item_grp.GroupName,
                ButtonID = item_btn.ButtonID,
                Default_TaxId = item_grp.Default_TaxId,
                Out_Of_Sales = item_grp.Out_Of_Sales,
                Sales_Seq = item_grp.Sales_Seq,
                DataModel_Button = new DM_Button(),
              }).ToArray<DM_ItemGroup>();

I initially tried .ToString() but it gave an exception. After trying SqlFunctions.StringConvert I am getting the following error:-

The specified method 'System.String StringConvert(System.Nullable`1[System.Decimal])'
on the type 'System.Data.Objects.SqlClient.SqlFunctions'
cannot be translated into a LINQ to Entities store expression.

How to convert the GroupID (which is a Int in my Table) to String (which is required by my DataModel)?

yacc
  • 2,915
  • 4
  • 19
  • 33
Jatinder Walia
  • 143
  • 1
  • 12
  • 1
    Possible duplicate of [LINQ to Entities StringConvert(double)' cannot be translated to convert int to string](https://stackoverflow.com/questions/5771299/linq-to-entities-stringconvertdouble-cannot-be-translated-to-convert-int-to-s) – CodeCaster Sep 17 '17 at 16:37
  • @CodeCaster:I have already looked at all these suggestions and have implemented the same,but haven't worked for me.Hence,please provide me a solution. – Jatinder Walia Sep 17 '17 at 16:38
  • Oh yeah I missed the "MySQL" part. You've found that SqlFunctions are SQL Server specific, right? – CodeCaster Sep 17 '17 at 16:40
  • @CodeCaster:can u now help me withe the solution? – Jatinder Walia Sep 17 '17 at 16:44
  • Why don't you just try it with .ToString() and then check if you can avoid the exception? – yacc Sep 17 '17 at 16:51
  • @yacc:try doing it and lemme know if it works. – Jatinder Walia Sep 17 '17 at 16:55
  • @yacc:How will avoiding the exception ultimately lead to resolution??My logic can't proceed until i get the object list.I can even change my DataType in Database table from Int to varchar also,but would hate to do so since it is a primary key. – Jatinder Walia Sep 17 '17 at 17:02
  • What's puzzling me is that you get an exception while trying to derive a string from an int? Did you try ""+GroupID ? – yacc Sep 17 '17 at 17:04

1 Answers1

1

Thanks for down voting a perfectly legitimate question.Wish someone had answered the question with equal promptness.

I found the solution to my problem which is as follows:-

 using (HexaEntities hh=new HexaEntities())
            {
                var cc = hh.hexa_item_group.ToDictionary(k => k.GroupID, k => k);
                var lst = from l in cc
                          orderby l.Key
                          select new DM_ItemGroup {GroupID = l.Key.ToString(), GroupName = l.Value.GroupName,  Default_TaxId=l.Value.Default_TaxId,ButtonID=l.Value.ButtonID.Value,Out_Of_Sales=l.Value.Out_Of_Sales,Sales_Seq=l.Value.Sales_Seq };
                AllRecords = lst.ToList<DM_ItemGroup>();

            }
Jatinder Walia
  • 143
  • 1
  • 12