当我尝试更新视图模型时,在此上下文中仅支持原始类型(例如 Int32、String 和 Guid)

发布于 2024-10-20 15:38:38 字数 2083 浏览 2 评论 0原文

我在尝试编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陈年往事 2024-10-27 15:38:38

此行有一个问题:

 ItemDescription = itemTypes.FirstOrDefault(r=>r.StoreItemID==item.StoreItemID)
                            .ItemDescription,

由于您使用的是 FirstOrDefault,如果没有满足条件的项目,您将获得 null 作为引用类型的默认值,那么您将尝试访问 ItemDescription 时出现异常 - 如果总是至少有一个匹配项,则使用 First() 或检查并定义 ItemDescription 的默认属性值 如果没有则使用:

 ItemDescription = itemTypes.Any(r=>r.StoreItemID==item.StoreItemID) 
                           ? itemTypes.First(r=>r.StoreItemID==item.StoreItemID)
                                      .ItemDescription
                           : "My Default",

This line has a problem:

 ItemDescription = itemTypes.FirstOrDefault(r=>r.StoreItemID==item.StoreItemID)
                            .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",
琉璃梦幻 2024-10-27 15:38:38

如果 itemTypes 是 IEnumerable 那么它不能在您的查询中使用(这是错误消息告诉您的),因为查询提供程序不知道如何处理它。因此,假设 itemTypes 基于与 TransactionEntities 相同的数据库中的表,那么您可以使用联接来实现相同的目标:

using (var entities = new MyEntities())
{
    HttpContext.Current.Session["Transactions"] = result =
                (from item in new entities.TransactionEntries
                 join itemType in entities.ItemTypes on item.StoreItemID equals itemType.StoreItemID
                 select new TransactionViewModel()
                 {
                     ItemDescription = itemType.ItemDescription,
                     TransactionDate = item.PurchaseDate.Value,
                     TransactionAmount = item.TransactionAmount.Value,
                     CustomerName = rewards.CardID//TODO: Get customer name

                 }).ToList();
}

我不知道您的数据库的结构,但希望您明白这一点。

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:

using (var entities = new MyEntities())
{
    HttpContext.Current.Session["Transactions"] = result =
                (from item in new entities.TransactionEntries
                 join itemType in entities.ItemTypes on item.StoreItemID equals itemType.StoreItemID
                 select new TransactionViewModel()
                 {
                     ItemDescription = itemType.ItemDescription,
                     TransactionDate = item.PurchaseDate.Value,
                     TransactionAmount = item.TransactionAmount.Value,
                     CustomerName = rewards.CardID//TODO: Get customer name

                 }).ToList();
}

I don't know the structure of your database, but hopefully you get the idea.

疑心病 2024-10-27 15:38:38

由于 LINQ 查询中存在可为 null 的整数,因此出现此错误。
在我的查询中添加检查解决了我的问题。

有问题的查询:

var x = entities.MyObjects.FirstOrDefault(s => s.Obj_Id.Equals(y.OBJ_ID));

有问题的查询已解决:

var x = entities.MyObjects.FirstOrDefault(s => s.Obj_Id.HasValue && s.Obj_Id.Value.Equals(y.OBJ_ID));

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:

var x = entities.MyObjects.FirstOrDefault(s => s.Obj_Id.Equals(y.OBJ_ID));

query with problem solved:

var x = entities.MyObjects.FirstOrDefault(s => s.Obj_Id.HasValue && s.Obj_Id.Value.Equals(y.OBJ_ID));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文