I would like to add values to my DatagridTextcolumn from a Linq query.
What I have tried:
DataGridTextColumn test = new DataGridTextColumn();
test.Binding = new Binding("test");
dgServer.Columns.Add(test);
dgServer.Items.Add(new Person()
{
Status = Convert.ToBoolean(from a in db.Persons
from d in db.PersonDetails
where a.pID == d.pDID && a.State == "Active"
select a)
});
XAML
<DataGridTextColumn x:Name="test" Binding="{Binding}" Header="test"/>
I have an exception for the above code, saying
Unable to cast object of type 'System.Data.Objects.ObjectQuery`1 to type 'System.IConvertible'.
how to bind to a DatagridTextcolumn
binding from LINQ to SQL.
Kindly Help
This is problematic code -
Status = Convert.ToBoolean(from a in db.Persons
from d in db.PersonDetails
where a.pID == d.pDID && a.State == "Active"
select a)
The query inside will return IEnumerable of type Person
and you are trying to convert this to Boolean
which obviously won't compile since there is no implicit conversion
from IEnumerable<Person>
to Boolean
.
The problem is that your are trying to convert a linq query rather than the results of that query. The issue arises from Linq's deferred execution model. Try adding a 'FirstOrDefault()' method to the parameter of your ToBoolean call:
Status = Convert.ToBoolean((from a in db.Persons
from d in db.PersonDetails
where a.pID == d.pDID && a.State == "Active"
select a).FirstOrDefault())
Rohit also has a good point - you need to make sure than you can actually convert from type Person to Boolean
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.