Asp.net 基于实体框架的查询通知系统

Asp.net 基于实体框架的查询通知系统,asp.net,asp.net-mvc,database,entity-framework,asp.net-mvc-4,Asp.net,Asp.net Mvc,Database,Entity Framework,Asp.net Mvc 4,我正在开发一个MVC4应用程序,我建立了一个通知系统,向订阅公司的用户发送有关封条的消息,这样我就可以保存用户上次访问网站的时间和销售完成的时间,下次用户返回网站时,我会强迫用户上次使用所有产品封存时间,如果产品封存时间大于用户上次访问时间,则返回所有指定此条件的产品,但我无法获得用户订阅的公司,只能从该公司获取此产品这是我的数据库: 这就是我在实体框架中要做的: public ActionResult LastTimeVisedSales() { if (User.I

我正在开发一个
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");
    }
public ActionResult LastTimeVisedSales()
{
if(User.Identity.IsAuthenticated)
{
var UserCompaniesSubscribed=db.subscribeddetails.Include(sub=>sub.User)。Include(sub=>sub.Company)
.Where(sub=>sub.UserID==CurrentUserID).ToList();
var LastTimeVisetDate=db.UserProfiles.Where(p=>p.UserID==CurrentUserID)
.选择(用户=>新建)
{
lastTimeViset=user.LastLogin
}).SingleOrDefault();
var lastlogin=LastTimeVisetDate.lastTimeViset;
列表结果=新列表();
foreach(UserCompaniesSubscribed中的var d){
var ProductNewsLogIn=db.Products.Include(p=>p.SubCategory).Include(p=>p.Store)
.Where(p=>p.SalesTime>=lastlogin&&p.Store.Company.CompanyID==d.CompanyID)
.选择(p=>新产品索引视图模型
{
ProductID=p.ProductID,
ProductImageURL=p.ProductImageURL,
ProductName=p.ProductName,
价格=p.价格,
数量=p.数量,
销售额=p.销售额,
折扣=p.折扣,
NewPrice=p.NewPrice,
StoreName=p.Store.StoreName,
CategoryName=p.SubCategory.Category.CategoryName,
SubCategory名称=p.SubCategory.SubCategory名称,
CompanyName=p.Store.Company.CompanyName
}).SingleOrDefault();
结果.添加(ProductNewsLogIn);
}
返回Json(结果);
}
返回Json(“上次登录时没有封条”);
}
并且非常感谢您的帮助

请尝试此查询

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

注意,我们可以利用EF的关联来避免连接。 此外,如果在where子句中使用关联实体,则该实体在select子句中自动可用。您不需要执行显式的
Include
。e、 g.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();
}
我们甚至可以将上面的所有内容组合成一个巨大的查询,该查询还可以内联计算最后一次登录。为了清楚起见,你喜欢什么都行

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; }

    ...
}
公共类用户配置文件
{
//通过dbo.subscribedDetails在dbo.UserProfile和dbo.companys之间进行M2M
公共虚拟ICollection公司{get;set;}
...
}
公共类产品
{
//通过StoreID访问dbo.Stores的外键
公共虚拟存储{get;set;}
...
}
公共类商店
{
//通过CompanyID向dbo公司发送外键
公共虚拟公司公司{get;set;}
...
}