groupBy查询错误

本文关键字:错误 查询 groupBy | 更新日期: 2023-09-27 17:59:55

我们试图在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; }
}

groupBy查询错误

尝试通过删除价格来更改此部分

group s.Price by new to group s by new