-
September 14th, 2012, 09:15 AM
#1
InvalidCastException when getting first result from LINQ query
Greetings!
I am a complete newbie at LINQ. I am trying to get my first simple little application using it to run. It's built in Visual Studio 2008, using DevArt's LinqConnect product to talk to a PostgreSQL database. Here's the function:
Code:
private void dgvCharges_CellClick(object sender, DataGridViewCellEventArgs e)
{
long chargeNumber = Convert.ToInt32(dgvCharges[e.ColumnIndex, e.RowIndex].Value);
var oneChargeSet = from charge in m_context.Charges
where charge.Charge1 == chargeNumber
select charge;
int chargeCount = oneChargeSet.Count();
var oneCharge = oneChargeSet.First();
}
In the debugger, I see that chargeCount is 1, as expected. I believe that the First() function should return an object that contains all of the data members of the charge table in the database: Charge1, BuiltDate, LoadedDate, Cycle, and everything else. This code compiles and starts nicely. But when I invoke the First() method, I get an InvalidCastException. I get the same expression when I try to give First an explicit type:
Code:
private void dgvCharges_CellClick(object sender, DataGridViewCellEventArgs e)
{
long chargeNumber = Convert.ToInt32(dgvCharges[e.ColumnIndex, e.RowIndex].Value);
var oneChargeSet = from charge in m_context.Charges
where charge.Charge1 == chargeNumber
select charge;
int chargeCount = oneChargeSet.Count();
var oneCharge = oneChargeSet.First<AnnealContext.Charge>();
}
Obviously, I don't understand something basic about LINQ. How do I get this to work?
Thanks very much!
RobR
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|