我正在尝试进行一次查询以在5个不同的表中点击数据库,以便在一个viewmodel中发送到我的视图。这是我的数据类。
public partial class Table_Parts_Warehouse
{
public int CQPN { get; set; }
public int CATEGORY { get; set; }
public int SUBCATEGORY { get; set; }
public string DESCRIPTION { get; set; }
public int QTY { get; set; }
public Nullable<int> MINSTK { get; set; }
public Nullable<int> MAXSTK { get; set; }
public Nullable<decimal> COST { get; set; }
}
public partial class Table_Parts_Order
{
public int ORDERID { get; set; }
public System.DateTime DATE { get; set; }
public bool STATUS { get; set; }
public string PONO { get; set; }
public string INVOICENO { get; set; }
public string QUOTENO { get; set; }
public int COMPANYID { get; set; }
public Nullable<decimal> COST { get; set; }
}
public partial class Table_Parts_OrderPart
{
public int ORDERPARTID { get; set; }
public int ORDERID { get; set; }
public int CQPN { get; set; }
public int ORDERQTY { get; set; }
public Nullable<int> RECVQTY { get; set; }
public string PARTNO { get; set; }
}
public partial class Table_Parts_Vendor
{
public int COMPANYID { get; set; }
public string PHONENO { get; set; }
public string COMPANYNAME { get; set; }
}
public partial class Table_Parts_Part
{
public int PARTID { get; set; }
public int CQPN { get; set; }
public string PARTNO { get; set; }
public decimal COST { get; set; }
public int COMPANYID { get; set; }
public int QTY { get; set; }
}
我的viewmodel类在这里,所有这些类,订单类是单个订单条目,但其余的都是该订单的关联零件和数据的列表。
public class OrderDetailsViewModel
{
public Table_Parts_Order tb_parts_order { get; set; }
public List<Table_Parts_OrderPart> tb_parts_orderpart { get; set; }
public List<Table_Parts_Vendor> tb_parts_vendor { get; set; }
public List<Table_Parts_Warehouse> tb_parts_warehouse { get; set; }
public List<Table_Parts_Part> tb_parts_part { get; set; }
}
在我的视图控制器中,我尝试对所有这些进行1次查询并一次性选择它们。我可以使用相同的查询单独执行此操作并选择表,然后.ToList()工作正常,但我打了5次而不是一次。需要帮助来选择此查询中的列表。
public ActionResult PartsOrderDetails(int? id)
{
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
OrderDetailsViewModel vm = new OrderDetailsViewModel();
vm = (from a in db.Table_Parts_Warehouse
join b in db.Table_Parts_OrderPart
on a.CQPN equals b.CQPN
join c in db.Table_Parts_Part
on b.PARTNO equals c.PARTNO
join d in db.Table_Parts_Vendor
on c.COMPANYID equals d.COMPANYID
join e in db.Table_Parts_Order
on b.ORDERID equals e.ORDERID
orderby a.CQPN ascending
where b.ORDERID == id
select new OrderDetailsViewModel
{
tb_parts_order = e,
tb_parts_orderpart = b, //(.ToList)
tb_parts_vendor = d, //(.ToList)
tb_parts_warehouse = a, //(.ToList)
tb_parts_part = c //(.ToList)
});
return View(vm);
}
这显然不起作用,因为我无法获得这样的列表。我已经看到人们在select new中进行另一个查询,但我不想重复查询5次。什么是实现这一目标的最佳方式?
答案 0 :(得分:1)
这是我以前的项目的代码片段。你可以从这个例子中得到一个好主意。最好不要在控制器中进行查询以进行调试。我创建了一个服务类。
服务类
public static List<RetrivalQueryModel> GetPartner()
{
List<RetrivalQueryModel> partnerList = new List<RetrivalQueryModel>();
using (SqlConnection connection = new SqlConnection(_constring))
{
StringBuilder sqlCommandBuilder = new StringBuilder();
sqlCommandBuilder.Append("SELECT Partner.PartnerId, (Staff.Forename+ ','+Staff.Surname) AS Name FROM tblpartner Partner JOIN tblstaff Staff ON Staff.StaffId = Partner.StaffId Where Partner.Suspended = 0 ORDER BY Staff.Forename , Staff.Surname "); //will search only for active partners, take away Partner.Suspended to show all
SqlCommand sqlCommand = new SqlCommand(sqlCommandBuilder.ToString(), connection);
try
{
connection.Open();
SqlDataReader reader = sqlCommand.ExecuteReader();
while (reader.Read())
{
try
{
RetrivalQueryModel partner = new RetrivalQueryModel();
partner.Partner = reader.GetString(1);
partner.PartnerId = reader.GetInt32(0);
partnerList.Add(partner);
}
catch (Exception ex)
{
log.Error(ex);
}
}
reader.Close();
connection.Close();
}
catch (Exception ex)
{
log.Error(ex);
}
}
return partnerList;
}
控制器
public JsonResult GetPartnerList()
{
List<RetrivalQueryModel> rtnList = PMService.GetPartner();
JsonResult result = new JsonResult();
result.Data = rtnList;
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return result;
}
<强>模型强>
namespace EngagementRetrival.Models
{
public class RetrivalQueryModel
{
public string JobStatus { get; set; }
public int JobStatusID { get; set; }
public string Market { get; set; }
public int MarketId { get; set; }
public string Office { get; set; }
public int OfficeId { get; set; }
public string Partner { get; set; }
public int PartnerId { get; set; }
}
}
答案 1 :(得分:0)
对于任何关心的人,我都想到了这一点。实际上很容易。请勿使用select new OrderDetailsViewModel尝试在您的linq查询中执行此操作。只需将查询作为列表抓取,迭代它们并添加到列表中。
public ActionResult PartsOrderDetails(int? id)
{
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
OrderDetailsViewModel vm = new OrderDetailsViewModel();
var query = (from a in db.Table_Parts_Warehouse
join b in db.Table_Parts_OrderPart
on a.CQPN equals b.CQPN
join c in db.Table_Parts_Part
on b.PARTNO equals c.PARTNO
join d in db.Table_Parts_Vendor
on c.COMPANYID equals d.COMPANYID
orderby a.CQPN ascending
where b.ORDERID == id
select new
{
tb_parts_orderpart = b,
tb_parts_vendor = d,
tb_parts_warehouse = a,
tb_parts_part = c
}).ToList();
vm.tb_parts_order = db.Table_Parts_Order.Find(id);
vm.tb_parts_orderpart = new List<Table_Parts_OrderPart>();
vm.tb_parts_part = new List<Table_Parts_Part>();
vm.tb_parts_vendor = new List<Table_Parts_Vendor>();
vm.tb_parts_warehouse = new List<Table_Parts_Warehouse>();
for (int i = 0; i < query.Count(); i++)
{
vm.tb_parts_orderpart.Add(query[i].tb_parts_orderpart);
vm.tb_parts_part.Add(query[i].tb_parts_part);
vm.tb_parts_vendor.Add(query[i].tb_parts_vendor);
vm.tb_parts_warehouse.Add(query[i].tb_parts_warehouse);
}
return View(vm);
}