简体   繁体   English

使用IQueryable过滤使用ID的gridview

[英]using IQueryable to filter gridview using ids

Hi and thanks in advance, 嗨,谢谢你,

I have several functions that alter my gridview. 我有几个函数可以更改我的gridview。 So to know whats in the current view, I wanted keep track of the ids. 因此,要了解当前视图中的内容,我想跟踪ID。 Current code below: 当前代码如下:

public void getCurrent_GridView()
{

    if (GridViewFacility.DataKeys.Count > 0)
    {
        WISSModel.WISSEntities context = new WISSModel.WISSEntities();

        //Prepare to build a "Base" query:
        IQueryable<WISSModel.Base> searchListQuery = context.Bases;

        for (int i = 0; i < GridViewFacility.Rows.Count; i++)
        {
            String id = GridViewFacility.DataKeys[i].Value.ToString();

            //Refine query, one search term at a time
            searchListQuery =
            searchListQuery.Where(p => p.isDeleted == false && (p.BaseId.ToString().Contains(id)));


            //txtSearch.Text += id + " ";
        }

        txtSearch.Text = "test" + searchListQuery.ToList().Count;

        GridViewFacility.DataSource = searchListQuery.ToList();
        GridViewFacility.DataBind();
    }
}

protected void GridViewFacility_RowEditing(object sender, GridViewEditEventArgs e)
{

    getCurrent_GridView();
    GridViewFacility.EditIndex = e.NewEditIndex;
    LoadData();

}

In VS2013, I get this error: 在VS2013中,出现此错误:

An exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll but was not handled in user code System.Data.Entity.dll中发生类型为'System.NotSupportedException'的异常,但未在用户代码中处理

Additional information : LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression. 附加信息 :LINQ to Entities无法识别方法'System.String ToString()',并且该方法无法转换为商店表达式。

Well, the message is rather clear, you can't use .ToString() 好吧,消息很清楚,您不能使用.ToString()

In linq to entities, you can use SqlFunctions.StringConvert ... to convert some numeric values to string. 在linq to实体中,可以使用SqlFunctions.StringConvert ...将某些数字值转换为字符串。 It's only available for decimal and double, but you can cast an int to a double. 它仅适用于十进制和双精度,但您可以将int强制转换为双精度。

I guess your error is on the line 我想你的错误就在网上

p.BaseId.ToString().Contains(id)

If BaseId is an int, you can replace this by 如果BaseId是一个int,则可以将其替换为

SqlFunctions.StringConvert((double)p.BaseId).Contains(id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM