I have below code:
using (DBContext context = new DBContext())
{
myCollection = context.Items.Where(i => i.Type == 1).OrderBy(k => k.Name).Select(w => new
{
Alias = w.Name + string.Format("{0}", w.Id),
Name = w.Name
}).ToArray();
}
In runtime i get an error when trying to concatenate the strings and trying con convert integer w.Id to string.
Error says:
Linq to entities does not recognize method string.Format
also plus concatenation sign '+' is not supported.
I have solved this by introducing AsEnumerable:
using (DBContext context = new DBContext())
{
myCollection = context.Items.AsEnumerable().Where(i => i.Type == 1).OrderBy(k => k.Name).Select(w => new
{
Alias = w.Name + string.Format("{0}", w.Id),
Name = w.Name
}).ToArray();
}
but I would like to know if this is the best solution or there is another way more suitable for doing this. Ideas?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…