问题Linq中转换的int字符串实体
-
21-08-2019 - |
题
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#的严格!
解决方案
使用EF V4可以使用 SqlFunctions.StringConvert
。有没有过载对于int,所以你需要转换为双或小数。您的代码最终看起来像这样:
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到实体先执行查询,然后操纵刺在LINQ到对象。在本例中,我想以获得一组包含联系人的全名,和ContactLocationKey,这是两个整数的列(的ContactID和LocationID)的串concatination数据的
// 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();
}
}
var selectList = db.NewsClasses.ToList<NewsClass>().Select(a => new SelectListItem({
Text = a.ClassName,
Value = a.ClassId.ToString()
});
首先,转换为对象,然后的toString()将是正确的。
布赖恩Cauthon的答案是优秀的!只是一个小更新,EF 6,全班起立移动到另一个命名空间。因此,EF 6之前,您应该包括:
System.Data.Objects.SqlClient
如果更新到EF 6,或简单地使用这个版本中,包括:
System.Data.Entity.SqlServer
通过包括EF6不正确的命名空间,则代码将编译得很好,但会引发运行时错误。我希望本说明有助于避免一些混乱。
我碰到了同样的问题,当我我的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。
public class BtoSelectedListItem : SelectListItem
{
public int IntValue
{
get { return string.IsNullOrEmpty(Value) ? 0 : int.Parse(Value); }
set { Value = value.ToString(); }
}
}
一个更溶液:
c.ContactId + ""
只需添加空字符串,它会被转换成字符串。
如果您使用实体框架和你想的唯一INT接受的,那么你可以使用这个在LINQ查询,你可以试试这个
var items = from c in contacts
select new ListItem
{
Value = (int)ContractId
Text = c.Name
};
它会工作,因为使用(INT)将您的值转换成整数,所以你不需要任何转换字符串为int,你会得到你想要的结果。
这在我的项目工作对我来说,我认为这将是对你有所帮助。
我的理解是,你必须创建一个部分类为“扩展”模型,并添加属性,该属性为只读,可利用类的属性的其余部分。
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
};