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
};
反正我能做到这一点吗? 请注意,在 VB.NET 中使用第一个片段没有问题,它工作得很好,VB 很灵活,我无法适应 C# 的严格!!!
SqlFunctions.StringConvert
。 int 没有重载,因此您需要转换为 double 或 decimal。你的代码最终看起来像这样:
var items = from c in contacts
select new ListItem
{
Value = SqlFunctions.StringConvert((double)c.ContactId).Trim(),
Text = c.Name
};
我通过将整数到字符串的转换放在查询之外解决了类似的问题。这可以通过将查询放入对象来实现。
var items = from c in contacts
select new
{
Value = c.ContactId,
Text = c.Name
};
var itemList = new SelectList();
foreach (var item in items)
{
itemList.Add(new SelectListItem{ Value = item.ContactId, Text = item.Name });
}
使用 LinqToObject:联系人。AsEnumerable()
var items = from c in contacts.AsEnumerable()
select new ListItem
{
Value = c.ContactId.ToString(),
Text = c.Name
};
SqlFunctions.StringConvert 会工作,但我觉得它很麻烦,而且大多数时候,我真的不需要在 SQL 端执行字符串转换。
如果我想进行字符串操作,我会先在 linq-to-entities 中执行查询,然后在 linq-to-objects 中操作字符串。在这个例子中,我想获取一组包含联系人全名和 ContactLocationKey 的数据,ContactLocationKey 是两个 Integer 列(ContactID 和 LocationID)的字符串连接。
// perform the linq-to-entities query, query execution is triggered by ToArray()
var data =
(from c in Context.Contacts
select new {
c.ContactID,
c.FullName,
c.LocationID
}).ToArray();
// at this point, the database has been called and we are working in
// linq-to-objects where ToString() is supported
// Key2 is an extra example that wouldn't work in linq-to-entities
var data2 =
(from c in data
select new {
c.FullName,
ContactLocationKey = c.ContactID.ToString() + "." + c.LocationID.ToString(),
Key2 = string.Join(".", c.ContactID.ToString(), c.LocationID.ToString())
}).ToArray();
现在,我承认必须编写两个匿名选择确实很麻烦,但我认为这比您可以执行 L2E 不支持的字符串(和其他)函数的便利性更重要。另请记住,使用此方法可能会降低性能。
public static IEnumerable<SelectListItem> GetCustomerList()
{
using (SiteDataContext db = new SiteDataContext())
{
var list = from l in db.Customers.AsEnumerable()
orderby l.CompanyName
select new SelectListItem { Value = l.CustomerID.ToString(), Text = l.CompanyName };
return list.ToList();
}
}
Brian Cauthon 的回答非常好!只是一点点更新,对于 EF 6,该类已移至另一个命名空间。所以,在 EF 6 之前,你应该包括:
System.Data.Objects.SqlClient
如果您更新到 EF 6,或者只是使用此版本,请包括:
System.Data.Entity.SqlServer
通过在 EF6 中包含不正确的命名空间,代码可以正常编译,但会引发运行时错误。我希望这个注释有助于避免一些混淆。
var selectList = db.NewsClasses.ToList<NewsClass>().Select(a => new SelectListItem({
Text = a.ClassName,
Value = a.ClassId.ToString()
});
先转换成object,然后toString()就正确了
另一种解决方案:
c.ContactId + ""
只需添加空字符串,它就会被转换为字符串。
我在将我的 MVC 2 应用程序转换为 MVC 3 时遇到了同样的问题,只是为了给这个问题提供另一个(干净的)解决方案,我想发布我所做的......
IEnumerable<SelectListItem> producers = new SelectList(Services.GetProducers(),
"ID", "Name", model.ProducerID);
GetProducers() 简单地返回生产者的实体集合。 附言SqlFunctions.StringConvert 对我不起作用。
如果您的“联系人”充当通用列表,我希望以下代码能够正常工作。
var items = contact.Distinct().OrderBy(c => c.Name)
.Select( c => new ListItem
{
Value = c.ContactId.ToString(),
Text = c.Name
});
谢谢。
使用 MySql,
SqlFunctions.StringConvert
对我不起作用。由于我在项目中的 20 多个地方使用了 SelectListItem
,因此我想要一个能够在不扭曲 20 多个 LINQ 语句的情况下工作的解决方案。我的解决方案是对 SelectedListItem
进行子类化,以提供一个整数设置器,它将类型转换从 LINQ 移开。显然,这个解决方案很难一概而论,但对我的具体项目很有帮助。
要使用,请创建以下类型并在 LINQ 查询中使用代替
SelectedListItem
并使用 IntValue 代替 Value.
public class BtoSelectedListItem : SelectListItem
{
public int IntValue
{
get { return string.IsNullOrEmpty(Value) ? 0 : int.Parse(Value); }
set { Value = value.ToString(); }
}
}
如果你使用实体框架并且你想让唯一的int可接受那么你可以在linq查询中使用它你可以试试这个
var items = from c in contacts
select new ListItem
{
Value = (int)ContractId
Text = c.Name
};
它会起作用,因为使用 (int) 会将您的值转换为 int,因此您不需要将字符串转换为 int,并且您会得到想要的结果。
这在我的项目中对我有用我认为这对你有帮助
14年后 这是另一种可能最简单的方法 使用
.ToString("D1")
例子
var items = from c in contacts
select new ListItem
{
Value = c.ContactId.ToString("D1"),
Text = c.Name
};
我的理解是,您必须创建一个部分类来“扩展”您的模型并添加一个只读属性,该属性可以利用该类的其余属性。
public partial class Contact{
public string ContactIdString
{
get{
return this.ContactId.ToString();
}
}
}
然后
var items = from c in contacts
select new ListItem
{
Value = c.ContactIdString,
Text = c.Name
};
var items = from c in contacts
select new ListItem
{
Value = String.Concat(c.ContactId), //This Works in Linq to Entity!
Text = c.Name
};
我发现
SqlFunctions.StringConvert((double)c.Age)
对我不起作用,或者该字段的类型是 Nullable<Int32>
在过去几天的反复试验中,我进行了大量搜索才找到这个。
我希望这能帮助一些编码员。
你能试试吗:
var items = from c in contacts
select new ListItem
{
Value = Convert.ToString(c.ContactId),
Text = c.Name
};