C# MVC核心存储库一次查询所有数据
我们有一个客户事务表,其中包含多个带外键的查找表。我们希望看到3张桌子连接在一起C# MVC核心存储库一次查询所有数据,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,我们有一个客户事务表,其中包含多个带外键的查找表。我们希望看到3张桌子连接在一起 public class CustomerTransaction { public int CustomerTransactionId{ get; set; }, public int ProductTypeId {get; set; }, //joins to ProductTypeTable public int StatusID {get; set; }, //joins to St
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}
我如何确保服务电话一个接一个?我不想获取所有不必要的客户事务,然后在内存中进行过滤。我想进行一次服务调用,以在一次sql查询中查找>50的客户事务
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}
存储库:
CustomerTransaction GetTransactionbyCustomerId(int customerid)
{
var result = ct.CustomerTransaction.Where(x => x.CustomerTransactionId == customerid).ToList()
return result;
}
void GetByCustomerTransactionGreaterthan50(int id)
{
var newdata = CustomerTransaction.GetByCustomerTransactionId();
nt.newdata.Where(x => x.PurchaseAmount > 50).ToList()
return newdata;
}
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}
服务电话:
CustomerTransaction GetTransactionbyCustomerId(int customerid)
{
var result = ct.CustomerTransaction.Where(x => x.CustomerTransactionId == customerid).ToList()
return result;
}
void GetByCustomerTransactionGreaterthan50(int id)
{
var newdata = CustomerTransaction.GetByCustomerTransactionId();
nt.newdata.Where(x => x.PurchaseAmount > 50).ToList()
return newdata;
}
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}
型号:
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}
您可以在
ToList()
之前进行Select
projection,这将确保EF生成的sql语句也只包含您要投影的字段
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}
比如:
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}
var result = ct.CustomerTransaction.Where(x => x.CustomerTransactionId == customerid).Select(x=> new CustomerTransaction()).ToList()
要进行筛选,您可以在where条件中包括筛选:
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}
ct.CustomerTransaction.Where(x => x.CustomerTransactionId == customerid && x.PurchaseAmount > 50)
或者让您的GetTransactionbyCustomerId像在服务调用中一样返回查询和筛选选项1
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}
您需要调整存储库,以便能够在服务层中添加查询:
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}
存储库:
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}
IQueryable<CustomerTransaction> QueryTransactionbyCustomerId(int customerid)
{
var result = ct.CustomerTransaction.Where(x => x.CustomerTransactionId == customerid);
return result;
}
List<CustomerTransaction> GetTransactionByCustomerIdAndAmount(int customerid, int amount)
{
var result = ct.CustomerTransaction.Where(x => x.CustomerTransactionId == customerid && x.PurchaseAmount > amount).ToList()
return result;
}
IQueryTableQueryTransactionByCustomerId(int-customerid)
{
var result=ct.CustomerTransaction.Where(x=>x.CustomerTransactionId==customerid);
返回结果;
}
选项2
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}
或在数据访问层中创建另一种方法:
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}
存储库:
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}
IQueryable<CustomerTransaction> QueryTransactionbyCustomerId(int customerid)
{
var result = ct.CustomerTransaction.Where(x => x.CustomerTransactionId == customerid);
return result;
}
List<CustomerTransaction> GetTransactionByCustomerIdAndAmount(int customerid, int amount)
{
var result = ct.CustomerTransaction.Where(x => x.CustomerTransactionId == customerid && x.PurchaseAmount > amount).ToList()
return result;
}
List getTransactionByCustomerId和amount(int-customerid,int-amount)
{
var result=ct.CustomerTransaction.Where(x=>x.CustomerTransactionId==customerid&&x.PurchaseAmount>amount).ToList()
返回结果;
}
根据您的体系结构,您应该选择以下选项之一。var result=ct.CustomerTransaction.Where(x=>x.CustomerTransactionId==customerid).ToList()-这将实现您的查询。您需要在此处返回IQueryable,并仅在服务调用中调用一次toList,不要尝试在“service”方法中筛选数据库行。在数据访问中这样做method@BlueLamp82不过,您没有创建通用存储库,而是在加载数据后尝试对其进行筛选。上下文确实提供了“通用”存储库所提供的功能。ViewModels是为*视图服务的,而不是为存储库服务的。@BlueLamp82是的,您可以而且应该拥有适合您的用例的尽可能多的DTO,而不是试图将一个大实体对象硬塞进所有角色中。如果需要仅从多个实体中选择5个属性,则应仅使用这5个属性创建一个DTO,而不是加载整个对象图。这是分离不同层和用例的唯一方法。您可以使用AutoMapper将实体映射到DTO到ViewModels,以确保如果实体映射失败,视图不会中断changes@BlueLamp82不要。这是IQueryable的工作。把它放在数据访问方法中。从该方法返回IQueryable如果您想应用额外的过滤器并延迟查询的执行,OP实际上会询问如何读取金额大于50的事务。这个问题的措辞很糟糕,被否决的人可能根本没有读过这个问题?这是一个措辞糟糕的问题。你不认为OP第一次是在寻找
包含吗?@PanagiotisKanavos谢谢,我也很困惑,但我留下我的答案,这可能会对OP有所帮助,让我们看看他会如何评论,让它更清楚。这要视情况而定。如果您公开IQueryable,您将有机会在另一层中添加到该查询中,但滥用这种行为是一种不好的做法。如果您选择选项2,您必须实现许多方法,并且您的存储库将根据您的域而增长。@BlueLamp82您所尝试的操作将确保您将在所有地方重复代码。使用IQueryable
意味着您不必这样做,只需对现有查询应用.Where()
。另外,您试图在错误的级别应用数据访问约束GetByCustomerTransactionGreater50
首先不应位于业务/服务层。@BlueLamp82如果要应用动态筛选条件并且不想将IQueryable返回到业务层,请向DAL传递一个默认值为null的Func
参数。如果参数不为null,请将其作为参数传递给。Where()
call@BlueLamp82数据层。内部GetTransactionbyCustomerId
。您可以在查询中根据需要链接任意多个.Where()
调用。将条件lambda作为Func
参数传递。如果它不为null,则在调用ToList()
之前添加另一个.Where()
,例如var query=ct.Transactions.Where(x=>CustomerId==a);if(filter!=null)query=query.Where(filter)代码>
public class CustomerTransaction
{
public int CustomerTransactionId{ get; set; },
public int ProductTypeId {get; set; }, //joins to ProductTypeTable
public int StatusID {get; set; }, //joins to StatusTypeTable
public string DateOfPurchase{ get; set; },
public int PurchaseAmount { get; set; },
}
public class ProductType
{
public int ProductTypeId{ get; set; }
public string ProductName { get; set; },
public string ProductDescription { get; set; },
}
public class StatusType
{
public int StatusId{ get; set; }
public string StatusName{ get; set; },
public string Description{ get; set; },
}