-2

String + Int + String is not working, when i assign the values to string column -> I Need the Name column value like :

AS Project Employee ID: 1878 Employee Name: Kevin dominic..

select new
        {
            ID = AS_ProjectHeader.ID,
            PartyID = AS_ProjectHeader.PartyID,
            Name = AS_ProjectHeader.Name + "  Employee ID: " + AS_ProjectHeader.EmployeeID + " Employee Name:" + AS_ProjectHeader.PAYE_Employee.Forenames + " " + AS_ProjectHeader.PAYE_Employee.Surname,
            ClientName = AS_ProjectHeader.AS_PartyList.Name,
            StartDate = AS_ProjectHeader.StartDate,
            EndDate = AS_ProjectHeader.EndDate,

        });

EDIT: Additional information:

If i use convert to string for the integer value i get this error :

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

Vj amal's
  • 9
  • 4
  • i don't think that you can do it every field should contain only 1 word/value – Marco Salerno Mar 22 '17 at 09:34
  • Try it with String.Format() – Alexander Powolozki Mar 22 '17 at 09:40
  • 2
    `... is not working` <= Please be more specific, include any exception and its details in your question. See also [ask] and please include an [mcve]. – Igor Mar 22 '17 at 09:40
  • "String + Int + String is not working," usually it is working. it results in a concatenated string – Mong Zhu Mar 22 '17 at 09:43
  • Igor .. I need the string + int + string values to be assigned to a string variable – Vj amal's Mar 22 '17 at 09:44
  • what is the output that you get? I think we got that point of what you need, you just did not describe what you really get when running your code. – Mong Zhu Mar 22 '17 at 09:44
  • Mong Zhu -- If i use convert to string for the integer value i get this error ---> LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression. – Vj amal's Mar 22 '17 at 09:45
  • `Igor .. I need the string + int + string values to be assigned to a string variable` <= No, I get that. What you did not provide though was what **is** happening. Simply stating "not working" could mean anything. Your computer could have exploded for all I know. We can't see your code, we can't see your output, and we are not mind readers. You have to be more specific. As mentioned please include the Exception (*from the original line of code*) and all its details OR describe what does occur if there is no exception. – Igor Mar 22 '17 at 09:47
  • welcome to StackOverflow, next time please use the [edit](http://stackoverflow.com/posts/42947555/edit) button below your post, to add further information to your post – Mong Zhu Mar 22 '17 at 09:49
  • 1
    Upgrade your EF version. – Gert Arnold Mar 22 '17 at 09:55
  • which framework are you using? – Mong Zhu Mar 22 '17 at 09:59
  • Mong Zhu .. version 4.5 – Vj amal's Mar 22 '17 at 10:00

3 Answers3

0

You can easily use .ToString()
but be-careful you could't use control like textBox1.Text inside Linq query

Ahmed Essawy
  • 326
  • 4
  • 13
  • If i use convert to string for the integer value i get this error ---> LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store – Vj amal's Mar 22 '17 at 09:54
  • So, try to declare new string variable before `Linq` query then assign `var_name.ToString();` then use `var_name` in your `Linq` query – Ahmed Essawy Mar 22 '17 at 09:57
  • let me try Ahmend – Vj amal's Mar 22 '17 at 10:06
  • Not working.. its works if the combination as string + Int but when it comes to String + int + string not working results in error as system.string method failed. – Vj amal's Mar 22 '17 at 10:46
0

This following is relevent only if using EF version older than 6.1

Linq-To-Entities really doesn't support ToString.

You have to materialize the query first (ToList()) and then select what you want.

So let say that query is what you have until the select.

query.ToList().Select(AS_ProjectHeader =>
{
    ID = AS_ProjectHeader.ID,
    PartyID = AS_ProjectHeader.PartyID,
    Name = AS_ProjectHeader.Name 
           + "  Employee ID: " + AS_ProjectHeader.EmployeeID 
           + " Employee Name:" + AS_ProjectHeader.PAYE_Employee.Forenames + " " 
                               + AS_ProjectHeader.PAYE_Employee.Surname,
    ClientName = AS_ProjectHeader.AS_PartyList.Name,
    StartDate = AS_ProjectHeader.StartDate,
    EndDate = AS_ProjectHeader.EndDate,
}   
Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27
0

Pleae Use SqlFunctions.StringConvert ,it will work

Name = PSC_ProjectHeader.Name + " Employee ID: " + SqlFunctions.StringConvert((decimal)PSC_ProjectHeader.EmployeeID) + " Employee Name:" + PSC_ProjectHeader.PAYE_Employee.Forenames + " " + PSC_ProjectHeader.PAYE_Employee.Surname,

Duraipandy
  • 26
  • 1
  • 2