1

when I trying to run following code.

var result = from c in db.brand
             where c.title.contains("test")
             select c.title + "-" +c.brand;

List<string> lst = r.ToList();

it gives following error.

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

Tim M.
  • 53,671
  • 14
  • 120
  • 163
Ali
  • 135
  • 2
  • 3
  • 12

3 Answers3

6

I would suggest fetching the title and the brand in an anonymous type, and then performing the string concatenation in-process:

var list = db.Brand.Where(c => c.Title.Contains("test"))
                   .Select(c => new { c.Title, c.Brand })
                   .AsEnumerable() // Rest of the query in-process
                   .Select(x => x.Title + " " + x.Brand)
                   .ToList();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

try this:

var result = from c in db.brand where c.title.contains("test") select c;
var finalResult = result.ToList().Select(ss=> ss.title + "-" + ss.brand);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 2
    You'd need to call `ToList` *again* to get a `List` which appears to be desired. Additionally, that will fetch *all* of the properties of each entity, when only the title and brand are really needed. Finally, there's no need to call `ToList` as the intermediate step - `AsEnumerable` can avoid creating the intermediate list. – Jon Skeet Apr 09 '12 at 09:10
  • ToList or AsEnumerable forces to execute the query over the datacontext after that String functions will be available to collection. Good catch Jon, thx – Yogesh Sharma Apr 09 '12 at 09:17
-1

try:

var result = from c in db.brand where c.title.contains("test") select new { c.title + "-" +c.brand }
Roger
  • 1,004
  • 2
  • 12
  • 24
  • 1
    Using an anonymous type isn't going to help here - and that code wouldn't even compile, as there's no name for the property in the anonymous type. – Jon Skeet Apr 09 '12 at 09:09
  • 1
    yes, you are right. did learn a lesson here: always test the answer! – Roger Apr 09 '12 at 09:13