System.linq.Dynamic.DynamicQuerable.OrderBy有一些无效的参数

时间:2014-10-18 07:37:06

标签: c# linq

尝试通过sting变量“sortCriteria”来排序多表连接的结果。代码在使用ERD的Sql版本中工作正常,但在此Xml版本中引发错误。谁能给我一些提示?谢谢

错误:' System.linq.Dynamic.DynamicQuerable.OrderBy(System.Linq.iQuerable,string,param object [])有一些无效的参数'


由于

 public ActionResult WebGrid(string sort = "OrderID", string sortDir = "ASC", int rowsPerPage = 10, int page = 1)
    {

        string path = System.Web.HttpContext.Current.Server.MapPath(@"~\App_Data");
        var Customers = XElement.Load(path + @"\XCustomers.xml").
    Elements("Customer").
    Select(x => new CustomerModel(
        (string)x.Element("CustomerID"),
        (string)x.Element("CompanyName"),
        (string)x.Element("ContactName")

    )).ToList();

        var employees=XElement.Load(path + @"\XEmployees.xml").
    Elements("Employee").
    Select(x => new EmployeeModel(
        (int)x.Element("EmployeeID"),
        (string)x.Element("FirstName"),
        (string)x.Element("LastName")

    )).ToList();

       var orderDetails = XElement.Load(path + @"\XOrderDetails.xml").
    Elements("OrderDetail").
    Select(x => new OrderDetailModel(
        (int)x.Element("OrderID"),
        (int)x.Element("Quantity"),
        (decimal)x.Element("UnitPrice")

    )).ToList();

        var orders = XElement.Load(path + @"\XOrders.xml").
    Elements("Order").
    Select(x => new OrderModel(
        (int)x.Element("OrderID"),
        (string)x.Element("CustomerID"),
        (int)x.Element("EmployeeID")

    )).ToList();


        ViewBag.sort = sort;
        ViewBag.sortDir = sortDir;
        ViewBag.rowsPerPage = rowsPerPage;
        ViewBag.page = page;
        ViewBag.count = orders.Count();

        var sortCriteria = string.Format("{0} {1}, OrderID {1}", sort, sortDir);

        var grouping = orderDetails.GroupBy(t => t.OrderID).Select(c => new { OrderID = c.Key, Price = c.Sum(w => w.UnitPrice), Quantity = c.Sum(e => e.Quantity) });



        var result = orders.Join(Customers, o => o.CustomerID, c => c.CustomerID, (o, c) =>
   new { o.OrderID, o.EmployeeID, c.CompanyName, c.ContactName }).Join(employees, oc => oc.EmployeeID, e => e.EmployeeID, (oc, e)
   => new { oc.OrderID, oc.CompanyName, oc.ContactName, Name = e.FirstName + " " + e.LastName, }).Join(grouping, oce => oce.OrderID,
   od => od.OrderID, (oce, od) => new MyModel
   {
       OrderID = oce.OrderID,
       ContactName = oce.ContactName,
       CompanyName = oce.CompanyName,
       EmployeeName = oce.Name,
       TotalQuantity = (int)od.Quantity,
       TotalPrice = (int)od.Price
   }).OrderBy(sortCriteria).Skip((page - 1) * rowsPerPage)
           .Take(rowsPerPage);
        return View(result );
    }

}

0 个答案:

没有答案