【问题标题】:groupBy query ErrorgroupBy 查询错误
【发布时间】:2015-06-25 14:41:34
【问题描述】:

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

【问题讨论】:

  • 这个错误没有什么意义......这实际上是逐字逐句地收到您收到的错误消息吗?
  • 是的,这是我们得到的错误,错误在第 50 行“ViewBag.cusT = cust.ToList();”
  • 在你做你的组之前调用 .ToList() 将它从 ef 中拉出来它似乎在 sql 中执行它时遇到了麻烦
  • @johnny5 它不起作用。

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


【解决方案1】:

尝试通过删除 Price 来更改这部分

group s.Price by new to group s by new

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-01
    • 2021-11-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多