当我尝试更新视图模型时,在此上下文中仅支持原始类型(例如Int32,String和Guid) [英] Only primitive types ('such as Int32, String, and Guid') are supported in this context when I try updating my viewmodel
问题描述
我要编写的linq查询遇到一些麻烦.
I am having some trouble with a linq query I am trying to write.
我正在尝试使用存储库模式,但运气不佳.基本上,我有一个交易列表和一个第二个列表,其中包含描述字段,该描述字段与我的案例StoreItemID中的字段相对应
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;
但是我收到此错误
无法创建类型为'MyMVC.ViewModels.BusinessItemViewModel'的常量值.在这种情况下,仅支持基本类型(例如Int32,String和Guid).
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,
如何将ItemDescription映射到我的itemTypes列表上?
How can I map ItemDescription against my list of itemTypes?
任何帮助都会很棒:)
推荐答案
此行有问题:
ItemDescription = itemTypes.FirstOrDefault(r=>r.StoreItemID==item.StoreItemID)
.ItemDescription,
由于您使用的是FirstOrDefault
,因此如果没有满足条件的项目,则会将null
作为参考类型的默认值,然后在尝试访问ItemDescription
时会遇到异常-要么使用First()
如果始终存在至少一个匹配项,或者检查并定义默认属性值,以供ItemDescription
在没有属性时使用:
Since you are using FirstOrDefault
you will get null
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 access ItemDescription
- either use First()
if there always will be at least one match or check and define a default property value for ItemDescription
to use if there is none:
ItemDescription = itemTypes.Any(r=>r.StoreItemID==item.StoreItemID)
? itemTypes.First(r=>r.StoreItemID==item.StoreItemID)
.ItemDescription
: "My Default",
这篇关于当我尝试更新视图模型时,在此上下文中仅支持原始类型(例如Int32,String和Guid)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!