简体   繁体   中英

using IQueryable to filter gridview using ids

Hi and thanks in advance,

I have several functions that alter my gridview. So to know whats in the current view, I wanted keep track of the ids. 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:

An exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll but was not handled in user code

Additional information : LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

Well, the message is rather clear, you can't use .ToString()

In linq to entities, you can use SqlFunctions.StringConvert ... to convert some numeric values to string. It's only available for decimal and double, but you can cast an int to a double.

I guess your error is on the line

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

If BaseId is an int, you can replace this by

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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