1

几天来,我一直在努力尝试用我的 ASP.Net 应用程序实现 jQuery Bootgrid。到目前为止,这就是我所拥有的:(按功能排序还没有工作,我稍后会解决)

public JsonResult IndexJson(RequestData model)
{
    var result = (from x in db.ContactSet
        select new
        {
            x.AccountId,
            x.FirstName,
            x.LastName,
            x.FullName,
            x.JobTitle,
            x.ParentCustomerId,
            x.EMailAddress1,
            x.Telephone1,
            x.MobilePhone,
            x.Fax,
            x.GenderCode,
            x.BirthDate
        }); //? Gets all rows

    result = (from x in result
        where x.FirstName.Contains(model.searchPhrase)
            || x.LastName.Contains(model.searchPhrase)
        select x); //? Search Filter

    var totalRows = result.Count(); //? Sets totalRows (for ResponseData)

    if (model.rowCount == -1)
        model.rowCount = totalRows; //? In case View All Rows is selected by Bootgrid (for ResponseData)

    // TODO: Add Order By functionality

    var tResult = new ResponseData<object>()
    {
        current = model.current,
        rowCount = model.rowCount,
        rows = result.ToList(),
        total = totalRows
    }; //? Builds Json Response

    return Json(tResult, JsonRequestBehavior.AllowGet);
}

这段代码的问题是我需要在搜索功能之后计算记录总数,而我只是不擅长正确使用 LINQ 查询。

当我到达时,var totalRows = result.Count();我收到以下错误:

System.NotSupportedException: '方法'Where'不能遵循方法'Select'或不受支持。尝试根据支持的方法编写查询,或者在调用不受支持的方法之前调用“AsEnumerable”或“ToList”方法。

知道这里有什么问题吗?

4

1 回答 1

0

我一直在不同情况下使用bootgrid,包括实现服务器端分页和排序asc、desc,没有任何问题。

尝试这个:

//Let's assume this is your model....
public class RequestData
{
    public int RowCount { get; set; }
    public int Current { get; set; }
    public string Search { get; set; }
    public string SortBy { get; set; }
    public string SortDirection { get; set; }
    public int TotalItems { get; set; }
}

1.如果您没有选择数据库表的所有列,请创建一个 DTO 来映射您选择的列。否则跳过这部分并用 ContactSet 替换你看到的任何地方 ContactSetDTO

public class ContactSetDTO
{
    public string AccountId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get; set; }         
    public string JobTitle { get; set; }         
    public string ParentCustomerId { get; set; }
    public string EMailAddress1 { get; set; }
    public string Telephone1 { get; set; }
    public string MobilePhone { get; set; }
    public string Fax { get; set; }
    public string GenderCode { get; set; }
    public DateTime BirthDate { get; set; }
}

2.假设您使用的是 SQL Server,您可以使用以下方法检索计数:

 public int getContactSetCount(string searchPhrase)
 {
    int ret = 0;
    try
    {
        string query = string.Empty;
        if (!string.IsNullOrWhiteSpace(searchPhrase))
        {
            // ********* Assuming your db table is also called ContactSet **********************
            query = @"SELECT COUNT(*) FROM ContactSet s WHERE s.FirstName LIKE '%' + @p0 + '%' OR s.LastName LIKE '%' + @p0 + '%')";
            ret = db.Database.SqlQuery<int>(query, new System.Data.SqlClient.SqlParameter(parameterName: "@p0", value: searchPhrase)).FirstOrDefault();
        }
        else
        {
            ret = db.ContactSet.Count();
        }
    }
    catch (Exception)
    {
         throw;
    }

    return ret;
}

3.最后,您的方法将如下所示:

public JsonResult IndexJson(RequestData model)
{
     var searchPhrase = model.Search;

     if (!string.IsNullOrWhiteSpace(searchPhrase))
     {
         //Notice that the select columns match the ContactSetDTO properties
         string query = @"SELECT TOP " + model.RowCount + " s.AccountId, s.FirstName, s.LastName, s.FullName, s.JobTitle, s.ParentCustomerId, s.EmailAddress1, s.Telephone1, s.MobilePhone, s.Fax, s.GenderCode, s.BirthDate FROM ContactSet s WHERE s.FirstName LIKE '%' + @p0 + '%' OR s.LastName LIKE '%' + @p0 + '%')";

   //Then, this should return a list of ContactSetDTO for you
    var result = db.Database.SqlQuery<ContactSetDTO>(query, new System.Data.SqlClient.SqlParameter(parameterName: "@p0", value: searchPhrase)).ToList();

     var totalRows = getContactSetCount(searchPhrase);

     var tResult = new { rows = result, rowCount = model.RowCount, total = totalRows, current = model.Current, searchPhrase = model.Search }
    };

    return Json(tResult, JsonRequestBehavior.AllowGet);
}

我希望以上内容会有所帮助。

于 2017-06-27T16:52:01.323 回答