Search code examples
c#asp.netlinq

Anonymous Type Appears in both Error


I have a linq query that populates the GridView on Page_Load. I have made a for loop of Characters for the alphabet. In the .Command of the LinkButton that populates the LinkButton, I am running a very similar query using the same parameters in the query and getting the below error.

The type '<>f__AnonymousType2' exists in both 'ConcernContracts.dll' and 'System.Web.WebPages.Deployment.dll'

void lnkCharacter_Command(object sender, CommandEventArgs e)
{
    try
    {
        var lbtn = (LinkButton)lbl_Alphabet.FindControl("lnkCharacter" + e.CommandArgument);
        var id = lbtn.Text;

        using (var db = new dbDataContext())
        {
            var query = from n in db.tbl_Providers
                        where ((n.provider_Name.StartsWith(id)) && (n.provider_Deleted == false))
                        select new
                        {
                            n.ProviderId,
                            n.provider_Name
                        };

            grd_Provider.DataSource = null;
            grd_Provider.DataSource = query;
            grd_Provider.DataBind();
        }
    }
    catch (SystemException ex) { }
}

The LoadGrid() is the same, but it doesn't use the .StartsWith() condition. Do you have any Ideas how to solve the error?

The error doesn't throw an exception, but it doesn't populate the grid for either of the queries. The error was discovered in the following line: grd_Provider.DataSource = query;


Solution

  • Change your grid data source

    grd_Provider.DataSource = query.ToList();
    grd_Provider.DataBind();
    

    or create List having two properties Provider Id and Name and bind that list from output like this.

     List<Entities> abc=query.ToList();
     grd_Provider.DataSource =abc;
     grd_Provider.DataBind();