groupBy查询错误

时间:2015-06-25 14:41:34

标签: c# sql asp.net .net asp.net-mvc

我们正在尝试在viewBag中返回一个查询,我们得到错误:

  

键切换类型的读取方法,GroupBy,不具备可比性的基本数据库提供程序。

这是我们的控制器:

public ViewResult Index(string sortOrder, string searchString, decimal Searchprice = -1)
        {
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "NameDesc" : "";
            ViewBag.ProductSortParm = sortOrder == "NameP" ? "NamePDesc" : "NameP";
            ViewBag.CompanySortParm = sortOrder == "NameS" ? "NameSDesc" : "NameS";
            var customers = from s in db.Customers
                           select s;
            if (!String.IsNullOrEmpty(searchString))
            {
                customers = customers.Where(s => s.Name.ToUpper().Contains(searchString.ToUpper())
                                       || s.NameP.ToUpper().Contains(searchString.ToUpper()));
            }
            if (!Searchprice.Equals(-1))
            {
                var cust = from s in db.Products
                           where s.Price < Searchprice
                           join sa in db.SupPro on s.ProductID equals sa.ProductID
                           join f in db.Customers on sa.CustomerID equals f.CustomerID
                           group s.Price by new
                           {
                               f.CustomerID,
                               f.Name,
                               f.NameP,
                               f.NameS,
                               f.Phone,
                               f.Address,
                               f.Email,
                               f.SupPro
                           } into Ncust
                           select new Customer { CustomerID = Ncust.Key.CustomerID, NameS = Ncust.Key.NameS, NameP = Ncust.Key.NameP, Name = Ncust.Key.Name, Phone = Ncust.Key.Phone, Address = Ncust.Key.Address, Email = Ncust.Key.Email, SupPro = Ncust.Key.SupPro };
                ViewBag.cusT = cust.ToList();
            }

这是我们的观点:

@model IEnumerable<application_project.Models.Customer>
@{
    ViewBag.Title = "Index";
    var fromView = ViewBag.cusT as IEnumerable<application_project.Models.Customer>;
}


<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@using (Html.BeginForm())
{
    <p>
        Price:  @Html.TextBox("Searchprice")
        Customer Name: @Html.TextBox("SearchString")  
        <input type="submit" value="Search" /></p>
}
@if (fromView==null)
{
<table class="table">
    <tr>
        <th>
            @Html.ActionLink("Name", "Index", new { sortOrder = ViewBag.NameSortParm })
        </th>
        <th>
            @Html.ActionLink("Product", "Index", new { sortOrder = ViewBag.ProductSortParm })
        </th>
        <th>
            @Html.ActionLink("Company", "Index", new { sortOrder = ViewBag.CompanySortParm })
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Phone)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Address)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)   
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NameP)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NameS)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Phone)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.CustomerID }) |
            @Html.ActionLink("Details", "Details", new { id=item.CustomerID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.CustomerID })
        </td>
    </tr>
}
</table>
}
else
{
   <table class="table">
    <tr>
        <th>
            @Html.ActionLink("Name", "Index", new { sortOrder = ViewBag.NameSortParm })
        </th>
        <th>
            @Html.ActionLink("Product", "Index", new { sortOrder = ViewBag.ProductSortParm })
        </th>
        <th>
            @Html.ActionLink("Company", "Index", new { sortOrder = ViewBag.CompanySortParm })
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Phone)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Address)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th></th>
    </tr>

@foreach (var item in fromView)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)   
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NameP)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NameS)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Phone)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.CustomerID }) |
            @Html.ActionLink("Details", "Details", new { id=item.CustomerID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.CustomerID })
        </td>
    </tr>
}
</table> 
}

这是我们的客户模式:

public class Customer
{
    [Key]
    public int CustomerID { get; set; }
    public String NameS { get; set; }
    public String NameP { get; set; }
    public String Name { get; set; }
    public String Phone { get; set; }
    public String Address { get; set; }
    public String Email { get; set; }

    public virtual ICollection<SupPro> SupPro { get; set; }
}

1 个答案:

答案 0 :(得分:0)

尝试通过删除价格

来更改此部分
group s.Price by new to group s by new