相关疑难解决方法(0)

将Linq转换为实体中的字符串的问题

var items = from c in contacts
            select new ListItem
            {
                Value = c.ContactId, //Cannot implicitly convert type 'int' (ContactId) to 'string' (Value).
                Text = c.Name
            };
var items = from c in contacts
            select new ListItem
            {
                Value = c.ContactId.ToString(), //Throws exception: ToString is not supported in linq to entities.
                Text = c.Name
            };
Run Code Online (Sandbox Code Playgroud)

无论如何我能做到这一点吗?注意,在VB.NET中没有问题使用它工作得很好的第一个片段,VB很灵活,我无法习惯C#的严格!

c# asp.net linq-to-entities tostring

199
推荐指数
4
解决办法
19万
查看次数

Linq int to string

如何将int和int转换为字符串?以下都不起作用:

from s in ctx.Services
    where s.Code.ToString().StartsWith("1")
    select s

from s in ctx.Services
    where Convert.ToString(s.Code).StartsWith("1")
    select s

from s in ctx.Services
    where ((string)s.Code).ToString().StartsWith("1")
    select s
Run Code Online (Sandbox Code Playgroud)

编辑

我得到的错误是:

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

.net linq linq-to-entities

48
推荐指数
2
解决办法
6万
查看次数

标签 统计

linq-to-entities ×2

.net ×1

asp.net ×1

c# ×1

linq ×1

tostring ×1