C# onvert((double)c.ContactId).Trim()似乎不适用于使用System.Data.SQLite的SQLite。在Typw'System.Data.Objects.SqlClient.SqlFunctions'kann nicht中
C# onvert((double)c.ContactId).Trim()似乎不适用于使用System.Data.SQLite的SQLite。在Typw'System.Data.Objects.SqlClient.SqlFunctions'kann nicht中,c#,asp.net,linq-to-entities,tostring,C#,Asp.net,Linq To Entities,Tostring,onvert((double)c.ContactId).Trim()似乎不适用于使用System.Data.SQLite的SQLite。在Typw'System.Data.Objects.SqlClient.SqlFunctions'kann nicht中,Typw'System.Data.sqler'LINQ to Entities'übersetzt werden中的方法'System.String StringConvert(System.Nullable'1[System.double]
onvert((double)c.ContactId).Trim()似乎不适用于使用System.Data.SQLite的SQLite。在Typw'System.Data.Objects.SqlClient.SqlFunctions'kann nicht中,Typw'System.Data.sqler'LINQ to Entities'übersetzt werden中的方法'System.String StringConvert(System.Nullable'1[System.double])。(不能翻译成“LINQ to Entities”)回答得很好!请注意,从使用EF 6开始,该类被移动到另一个名称空间。因此,在EF 6之前,如果您更新到EF 6,或者只是使用此版本,那么应该包括:“System.Data.Objects.SqlClient”,通过在EF6中包含不正确的名称空间,代码可以正常编译,但会引发运行时错误。我希望这张便条能帮助你避免一些混乱。你测试过它了吗?阅读答案之前。是的,我已经用过了。它适用于MVC3、EF4、CTP5、SQL CE4。这似乎比装箱和使用StringConvert更优雅。但在这种情况下,您将从数据库中获取所有数据,然后假设您想在
返回list.ToList()之前对此列表进行一些筛选代码>!!当您无法访问SqlFunctions时,除此之外,您没有其他许多选项。但是,我会在我的查询中使用它:return(从db.Customers orderby l.CompanyName选择new{Id=l.CustomerID,Name=l.CompanyName}).AsEnumerable().select(c=>new-SelectListItem{Value=c.Id.ToString(),Text=c.Name}.ToList()代码>。这样做只能从数据库中获取id/名称(而不是所有客户属性),并使用数据库中更有效的索引进行排序。对我来说不起作用。它抛出异常“…System.String Concat(System.Object)
无法转换为存储表达式…”。对我也不起作用。我还得到“System.NotSupportedException:LINQ to Entities不识别方法'System.String Concat(System.Object)'方法,并且此方法无法转换为存储表达式。”不起作用-使用此答案[NotSupportedException:LINQ to Entities不识别方法'System.String Concat(System.Object)]'方法,此方法无法转换为存储表达式。]上面的代码将不起作用,因为它将抛出一个错误,提示“LINQ to Entities无法识别方法'System.String ToString(Int32)'方法,此方法无法转换为存储表达式。”谢谢。仅供参考,我正试图解决一个稍微不同的问题。我正在使用LINQ来创建实体/lambda,这很有效。我试图将Int转换为字符串,并使用“Contains”查找匹配结果-->ie db.contacts.AsEnumerable()。其中(c=>c.ContactId.ToString().Contains(searchitem)).ToList();如果您调用AsEnumerable
,您将在大型数据库上付出高昂的性能代价,因为它会将所有内容都存储在内存中IEnumerable
与IQueryable
相比速度较慢,因为后者只在数据库中执行。我不得不说,您的答案也很好。我升级到EF6,一直在到处寻找SQLFunction。你的回答为我指明了正确的方向。我要补充的是,您还需要对EntityFramework.SqlServer的引用(您可能只有对EntityFramework的引用)。返回的错误:System.NotSupportedException:无法将类型“System.Int64”强制转换为类型“System.Object”。LINQ to Entities仅支持强制转换EDM基元或枚举类型。这是解决此问题的一种方法,但请记住,这将增加执行时间,如果您有大量对象,则此foreach过于复杂。。。
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
};
var items = from c in contacts
select new ListItem
{
Value = Convert.ToString(c.ContactId),
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 = SqlFunctions.StringConvert((double)c.ContactId).Trim(),
Text = c.Name
};
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();
}
}
IEnumerable<SelectListItem> producers = new SelectList(Services.GetProducers(),
"ID", "Name", model.ProducerID);
// 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();
var selectList = db.NewsClasses.ToList<NewsClass>().Select(a => new SelectListItem({
Text = a.ClassName,
Value = a.ClassId.ToString()
});
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 });
}
var items = contact.Distinct().OrderBy(c => c.Name)
.Select( c => new ListItem
{
Value = c.ContactId.ToString(),
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
};
var items = from c in contacts.AsEnumerable()
select new ListItem
{
Value = c.ContactId.ToString(),
Text = c.Name
};
public class BtoSelectedListItem : SelectListItem
{
public int IntValue
{
get { return string.IsNullOrEmpty(Value) ? 0 : int.Parse(Value); }
set { Value = value.ToString(); }
}
}
var items = from c in contacts
select new ListItem
{
Value = (int)ContractId
Text = c.Name
};
System.Data.Objects.SqlClient
System.Data.Entity.SqlServer
c.ContactId + ""