LINQ查询父子表

时间:2018-09-05 19:15:50

标签: asp.net-mvc

我遇到的问题是这样的:

我的视图中有一个文本框,该文本框允许使用Orders表的搜索/过滤器功能。但是,我希望它也能够通过子表OrderDetails搜索并进行适当过滤。

搜索文本框适用于订单表,但不适用于订单明细。

我意识到这段代码可以缩减很多,但是我是新的代码。

可能有帮助的事情-我先做数据库

到目前为止,这是我的代码。

控制器:

EntitiesDB db = new EntitiesDB();

    #region INDEX MAIN PAGE
    [HttpGet]
    public ActionResult Index(string sortOrder, string currentFilter, int? page, string jobNumberDropDown, string requesitionerDropDown, string supplierDropDown, string dateRequestedDropDown, string dateRequiredDropDown, string searchString)
    {

        var getSupplierList = db.Suppliers.OrderBy(SL => SL.SupplierName).ToList();
        var getEmployeeList = db.Employees.OrderBy(EL => EL.FullName).ToList();

        SelectList listOfSuppliers = new SelectList(getSupplierList, "SupplierName", "SupplierName");
        SelectList listOfEmployees = new SelectList(getEmployeeList, "FullName", "FullName");

        ViewBag.supplierNames = listOfSuppliers;
        ViewBag.employeeNames = listOfEmployees;

        #region PAGINATION FUNCTION
        //Code for Paging
        ViewBag.CurrentSort = sortOrder;
        if (searchString != null)
        {
            page = 1;
        }
        else
        {
            searchString = currentFilter;
        }

        ViewBag.CurrentFilter = currentFilter;


        int pageSize = 10;
        int pageNumber = (page ?? 1);

        #endregion

        #region SEARCH FUNCTION
        //Creates the LINQ Query to select the Orders from the ORDERS table 

        //var orders = from o in db.Orders join c in db.OrderDetails on o.OrderID equals c.OrderID select new { orders = o.JobNumber, o.PurchaseOrder, c.Description, c.PartNumber, c.AngstromPartNumber };

        var orders = from o in db.Orders.Where(x => x.IsDeleted != true) select o; /*-****WORKING FOR ORDER TABLE*****/

        var orderDetails = from od in db.OrderDetails.Where(x => x.IsDeleted != true) select od;

        //List of Employees
        var requesitionerList = new List<string>();
        //List of Suppliers
        var supplierList = new List<string>();
        //List of Job Numbers
        var jobNumberList = new List<string>();
        //List of Dates Requested
        var dateRequestedList = new List<DateTime>();
        //List of Dates Required
        var dateRequiredList = new List<DateTime>();

        //Query the ORDERS table columns
        var requesitionerQry = from r in db.Orders.Where(x => x.IsDeleted != true) orderby r.Requesitioner select r.Requesitioner;
        var supplierQry = from s in db.Orders.Where(x => x.IsDeleted != true) orderby s.Supplier select s.Supplier;
        var jobNumberQry = from j in db.Orders.Where(x => x.IsDeleted != true) orderby j.JobNumber select j.JobNumber;
        var dateRequestedQry = from DRqst in db.Orders.Where(x => x.IsDeleted != true) orderby DRqst.DateRequested select DRqst.DateRequested;
        var dateRequiredQry = from DRqrd in db.Orders.Where(x => x.IsDeleted != true) orderby DRqrd.DateRequired select DRqrd.DateRequired;
        //Searches through the columns filters out NULLS
        var t = requesitionerQry.Distinct().ToList().Where(x => String.IsNullOrWhiteSpace(x) == false);
        var u = supplierQry.Distinct().ToList().Where(y => String.IsNullOrWhiteSpace(y) == false);
        var v = jobNumberQry.Distinct().ToList().Where(z => String.IsNullOrWhiteSpace(z) == false);
        var dRT = dateRequestedQry.Distinct().ToList().Where(z1 => String.IsNullOrWhiteSpace(z1.ToString()) == false);
        var dRD = dateRequiredQry.Distinct().ToList().Where(z2 => String.IsNullOrWhiteSpace(z2.ToString()) == false);
        //Searches through the columns and only pulls out one of each unique string
        requesitionerList.AddRange(t);
        supplierList.AddRange(u);
        jobNumberList.AddRange(v);
        dateRequestedList.AddRange(dRT);
        dateRequiredList.AddRange(dRD);
        //I believe this Grabs the list items and arranges them into a viewable anonamous object
        ViewBag.requesitionerDropDown = new SelectList(requesitionerList);
        ViewBag.supplierDropDown = new SelectList(supplierList);
        ViewBag.jobNumberDropDown = new SelectList(jobNumberList);
        ViewBag.dateRequestedDropDown = new SelectList(dateRequestedList);
        ViewBag.dateRequiredDropDown = new SelectList(dateRequiredList);

        //This defines which columns the text in the search textbox should locate
        if (!String.IsNullOrEmpty(searchString))
        {
            orders = orders.Where(a => a.JobNumber.Contains(searchString)||
            //a.Description.Contains(searchString) || a.PartNumber.Contains(searchString) || a.AngstromPartNumber.Contains(searchString) ||
            a.Supplier.Contains(searchString) || a.Reason.Contains(searchString) || a.Requesitioner.Contains(searchString) ||
            a.DateRequested.ToString().Contains(searchString) || a.DateRequired.ToString().Contains(searchString) ||
            a.ExpectedDate.ToString().Contains(searchString) || a.DateOrderPlaced.ToString().Contains(searchString) ||
            a.PurchaseOrder.ToString().Contains(searchString));                            


        }
        if (!String.IsNullOrEmpty(searchString))
        {
            orders = orders.Where(x => x.OrderDetails.Any(i => i.Description.Contains(searchString)));
        }

        //Search the REQUESITIONER column
        if (!String.IsNullOrEmpty(requesitionerDropDown))
        {
            orders = orders.Where(b => b.Requesitioner == requesitionerDropDown);
        }
        //Search the SUPPLIER column
        if (!String.IsNullOrEmpty(supplierDropDown))
        {
            orders = orders.Where(c => c.Supplier == supplierDropDown);
        }
        //Search the JOBNUMBER column
        if (!String.IsNullOrEmpty(jobNumberDropDown))
        {
            orders = orders.Where(d => d.JobNumber == jobNumberDropDown);
        }
        if (!String.IsNullOrEmpty(dateRequestedDropDown))
        {
            orders = orders.Where(d => d.DateRequested.ToShortDateString() == dateRequestedDropDown);
        }
        if (!String.IsNullOrEmpty(dateRequiredDropDown))
        {
            orders = orders.Where(d => d.DateRequired.ToShortDateString() == dateRequiredDropDown);
        }
        #endregion

        //List<Order> OrderList = db.Orders.ToList();

        var sortedOrders = orders.OrderBy(x => x.JobNumber);

        return View(sortedOrders.ToPagedList(pageNumber, pageSize));
    }
    #endregion

这是我的模特:

public partial class Order
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Order()
    {
        this.OrderDetails = new HashSet<OrderDetail>();
    }

    public System.Guid OrderID { get; set; }
    public Nullable<bool> IsDeleted { get; set; }
    public string JobNumber { get; set; }
    public string Supplier { get; set; }
    public string Reason { get; set; }
    public string Requesitioner { get; set; }
    public System.DateTime DateRequested { get; set; }
    public System.DateTime DateRequired { get; set; }
    public Nullable<bool> Urgent { get; set; }
    public System.DateTime ExpectedDate { get; set; }
    public System.DateTime DateOrderPlaced { get; set; }
    public string PurchaseOrder { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<OrderDetail> OrderDetails { get; set; }
}

public partial class OrderDetail
{
    public System.Guid OrderDetailID { get; set; }
    public Nullable<bool> IsDeleted { get; set; }
    public int Quantity { get; set; }
    public string Description { get; set; }
    public string PartNumber { get; set; }
    public string Notes { get; set; }
    public string Comments { get; set; }
    public Nullable<decimal> UnitPrice { get; set; }
    public string AngstromPartNumber { get; set; }
    public string Manufacturer { get; set; }
    public Nullable<int> BoneYard { get; set; }
    public System.Guid OrderID { get; set; }
    public Nullable<System.Guid> EmployeeID { get; set; }
    public Nullable<System.Guid> SupplierID { get; set; }

    public virtual Employee Employee { get; set; }
    public virtual Order Order { get; set; }
    public virtual Supplier Supplier { get; set; }
}

0 个答案:

没有答案
相关问题