Reputation: 1049
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;
Upvotes: 10
Views: 2483
Reputation: 63
Convert it into a List
or IEnumerable
, you just cannot pass anonymous objects as datasource to gridview. query.ToList();
You may convert the return type to
IEnumerable<object>
it can hold any anonymous type and gets easily bound as datasource
Upvotes: 0
Reputation: 169
I had the same issue and I added another property to the anonymous type and that resolved it.
Upvotes: 0
Reputation: 26
Linq does not support some functionality like .toDays(),.addDays(),.StartsWith() . So what you need to do is,First get the result without using .StartsWith() then try to apply some functionality to filter result StartsWith perticular id.
Upvotes: -1
Reputation: 4489
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();
Upvotes: 2
Reputation: 1470
Here is a suggestion:
Your two similar queries are probably overlapping on that anonymous type you are selecting in the LINQ query. On one and only one of the queries, change the select new to look like this:
select new
{
Id = n.ProviderId,
Name = n.provider_Name
};
If you do this, the anonymous types shouldn't conflict anymore, since the one you don't modify will use the default names.
Good luck, and I hope this helps!
Upvotes: 1