当我尝试更新视图模型时,在此上下文中仅支持原始类型(例如 Int32、String 和 Guid)
我在尝试编写 linq 查询时遇到一些问题。
我正在尝试使用存储库模式,但运气不佳。基本上我有一个交易列表和第二个列表,其中包含与我的案例 StoreItemID 中的字段映射的描述字段
public static IList<TransactionViewModel> All()
{
var result = (IList<TransactionViewModel>)HttpContext.Current.Session["Transactions"];
if (result == null)
{
var rewardTypes = BusinessItemRepository.GetItemTypes(StoreID);
HttpContext.Current.Session["Transactions"] =
result =
(from item in new MyEntities().TransactionEntries
select new TransactionViewModel()
{
ItemDescription = itemTypes.FirstOrDefault(r=>r.StoreItemID==item.StoreItemID).ItemDescription,
TransactionDate = item.PurchaseDate.Value,
TransactionAmount = item.TransactionAmount.Value,
}).ToList();
}
return result;
}
public static List<BusinessItemViewModel>GetItemTypes(int storeID)
{
var result = (List<BusinessItemViewModel>)HttpContext.Current.Session["ItemTypes"];
if (result == null)
{
HttpContext.Current.Session["ItemTypes"] = result =
(from items in new MyEntities().StoreItems
where items.IsDeleted == false && items.StoreID == storeID
select new BusinessItemViewModel()
{
ItemDescription = items.Description,
StoreID = items.StoreID,
StoreItemID = items.StoreItemID
}).ToList();
}
return result;
但是我收到此错误
无法创建“MyMVC.ViewModels.BusinessItemViewModel”类型的常量值。此上下文仅支持原始类型(“例如 Int32、String 和 Guid”)。
我知道这行代码就像我注释掉它一样工作正常
ItemDescription = itemTypes.FirstOrDefault(r=>r.StoreItemID==item.StoreItemID).ItemDescription,
如何将 ItemDescription 映射到我的 itemTypes 列表?
任何帮助都会很棒:)
I am having some trouble with a linq query I am trying to write.
I am trying to use the repository pattern without to much luck. Basically I have a list of transactions and a 2nd list which contains the description field that maps against a field in my case StoreItemID
public static IList<TransactionViewModel> All()
{
var result = (IList<TransactionViewModel>)HttpContext.Current.Session["Transactions"];
if (result == null)
{
var rewardTypes = BusinessItemRepository.GetItemTypes(StoreID);
HttpContext.Current.Session["Transactions"] =
result =
(from item in new MyEntities().TransactionEntries
select new TransactionViewModel()
{
ItemDescription = itemTypes.FirstOrDefault(r=>r.StoreItemID==item.StoreItemID).ItemDescription,
TransactionDate = item.PurchaseDate.Value,
TransactionAmount = item.TransactionAmount.Value,
}).ToList();
}
return result;
}
public static List<BusinessItemViewModel>GetItemTypes(int storeID)
{
var result = (List<BusinessItemViewModel>)HttpContext.Current.Session["ItemTypes"];
if (result == null)
{
HttpContext.Current.Session["ItemTypes"] = result =
(from items in new MyEntities().StoreItems
where items.IsDeleted == false && items.StoreID == storeID
select new BusinessItemViewModel()
{
ItemDescription = items.Description,
StoreID = items.StoreID,
StoreItemID = items.StoreItemID
}).ToList();
}
return result;
However I get this error
Unable to create a constant value of type 'MyMVC.ViewModels.BusinessItemViewModel'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
I know its this line of code as if I comment it out it works ok
ItemDescription = itemTypes.FirstOrDefault(r=>r.StoreItemID==item.StoreItemID).ItemDescription,
How can I map ItemDescription against my list of itemTypes?
Any help would be great :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此行有一个问题:
由于您使用的是
FirstOrDefault
,如果没有满足条件的项目,您将获得null
作为引用类型的默认值,那么您将尝试访问ItemDescription
时出现异常 - 如果总是至少有一个匹配项,则使用First()
或检查并定义ItemDescription 的默认属性值
如果没有则使用:This line has a problem:
Since you are using
FirstOrDefault
you will getnull
as default value for a reference type if there is no item that satifies the condition, then you'd get an exception when trying to accessItemDescription
- either useFirst()
if there always will be at least one match or check and define a default property value forItemDescription
to use if there is none:如果 itemTypes 是 IEnumerable 那么它不能在您的查询中使用(这是错误消息告诉您的),因为查询提供程序不知道如何处理它。因此,假设 itemTypes 基于与 TransactionEntities 相同的数据库中的表,那么您可以使用联接来实现相同的目标:
我不知道您的数据库的结构,但希望您明白这一点。
If itemTypes is IEnumerable then it can't be used in your query (which is what the error message is telling you), because the query provider doesn't know what to do with it. So assuming the that itemTypes is based on a table in the same db as TransactionEntities, then you can use a join to achieve the same goal:
I don't know the structure of your database, but hopefully you get the idea.
由于 LINQ 查询中存在可为 null 的整数,因此出现此错误。
在我的查询中添加检查解决了我的问题。
有问题的查询:
有问题的查询已解决:
I had this error due a nullable integer in my LINQ query.
Adding a check within my query it solved my problem.
query with problem:
query with problem solved: