【问题标题】:Querying Notification System with entity framework使用实体框架查询通知系统
【发布时间】:2014-10-08 15:05:41
【问题描述】:

我正在开发一个MVC4 应用程序,并且我构建了一个通知系统,向订阅公司的用户发送有关印章的消息,这样我就可以保存用户最后一次访问该网站的时间以及完成销售和进入的时间下次用户返回该站点时,我会告诉用户上次访问该站点的所有产品密封时间,如果产品密封时间大于用户上次访问它会返回所有指定此条件的产品,但我无法获取用户订阅的公司,只能从这些公司获取此产品这是我的数据库:

这就是我在实体框架中尝试做的事情:

public ActionResult LastTimeVisedSales()
    {
        if (User.Identity.IsAuthenticated)
        {

   var UserCompaniesSubscribed = db.SubscribDetails.Include(sub => sub.User).Include(sub => sub.Company)
                                   .Where(sub => sub.UserID == CurrentUserID).ToList();

   var LastTimeVisetDate = db.UserProfiles.Where(p => p.UserID == CurrentUserID)
                             .Select(user => new
                              {
                             lastTimeViset = user.LastLogin
                              }).SingleOrDefault();

  var lastlogin = LastTimeVisetDate.lastTimeViset;

  List<ProductsIndexViewModel> Result = new List<ProductsIndexViewModel>();

      foreach(var d in UserCompaniesSubscribed){

         var ProductNewsLogIn = db.Products.Include(p => p.SubCategory).Include(p => p.Store)
                                .Where(p => p.SalesTime >= lastlogin && p.Store.Company.CompanyID == d.CompanyID )
                                .Select(p => new ProductsIndexViewModel
                                {
                                    ProductID = p.ProductID,
                                    ProductImageURL = p.ProductImageURL,
                                    ProductName = p.ProductName,
                                    Price = p.Price,
                                    Quantity = p.Quantity,
                                    Sales = p.Sales,
                                    Discount = p.Discount,
                                    NewPrice = p.NewPrice,
                                    StoreName = p.Store.StoreName,
                                    CategoryName = p.SubCategory.Category.CategoryName,
                                    SubCategoryName = p.SubCategory.SubCategoryName,
                                    CompanyName = p.Store.Company.CompanyName
                                }).SingleOrDefault();

                Result.Add(ProductNewsLogIn);
                }

            return Json(Result);
        }

        return Json("no seals from last time log in");
    }

非常感谢您的任何帮助。

【问题讨论】:

    标签: asp.net asp.net-mvc database entity-framework asp.net-mvc-4


    【解决方案1】:
    var products = db.Products.Where(p => p.SalesTime > user.LastLogin && user.Companies.Contains(p.Store.Company));
    

    注意事项

    对于此类问题,最好发布 POCO,因为表列实际上与您用于查询的实体框架 API 几乎没有关系。因此,我根据您的列名和关系来猜测属性名称。它假设您的课程中有以下内容:

    public class UserProfile
    {
        // M2M between dbo.UserProfile and dbo.Companies through dbo.SubscribDetails
        public virtual ICollection<Company> Companies { get; set; }
    
        ...
    }
    
    public class Product
    {
        // Foreign key to dbo.Stores via StoreID
        public virtual Store Store { get; set; }
    
        ...
    }
    
    public class Store
    {
        // Foreign key to dbo.Companies via CompanyID
        public virtual Company Company { get; set; }
    
        ...
    }
    

    【讨论】:

      【解决方案2】:

      请试试这个查询。

      基本上,我们使用一个简单的查询来找到用户的最后登录日期。 然后查询用户已订阅的公司所属店铺的所有产品。

      请注意,我们可以利用 EF 的关联来避免连接。 此外,如果您在 where 子句中使用关联实体,则它在 select 子句中自动可用。你不需要做一个明确的Include。例如p.存储在下面。

      var userProfile = db.UserProfiles.SingleOrDefault(up => up.UserID == CurrentUserID);
      
      if (userProfile != null)
      {
       var lastLoginDate = userProfile.LastLogin; 
       var companyIds = db.Companies.Where(c => c.UserID == CurrentUserID).Select(c => c.CompanyID);
      
       var productViewModels = db.Products.Include(p => p.SubCategory)
                                          .Join(companyIds, p => p.Stores.CompanyID, c => c, (p, c) => p) 
                                          .Where(p => p.SalesTime >= lastLoginDate)
                                          .Select(p => new ProductsIndexViewModel
                                           {
                                            ProductID = p.ProductID,
                                            ProductImageURL = p.ProductImageURL,
                                            ProductName = p.ProductName,
                                            Price = p.Price,
                                            Quantity = p.Quantity,
                                            Sales = p.Sales,
                                            Discount = p.Discount,
                                            NewPrice = p.NewPrice,
                                            StoreName = p.Store.StoreName,
                                            CategoryName = p.SubCategory.Category.CategoryName,
                                            SubCategoryName = p.SubCategory.SubCategoryName,
                                            CompanyName = p.Store.Company.CompanyName
                                           }).ToList();
      }
      

      我们甚至可以将以上所有内容组合成一个巨大的查询,该查询还计算最后一次内联登录。为了清晰起见,随便你喜欢什么。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-03
        • 1970-01-01
        • 2015-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多