My code :
var db = Database.Open("dbase");
var result = db.Query("SELECT event_id, description, title, event_start, event_end
FROM event
WHERE event_start >= "+ start + " AND event_end <= "+ @end);
foreach(var record in result)
{
CalendarEvent cevent = new CalendarEvent();
cevent.id = result.event_id; //error is thrown here
}
Error message :
CS1061: ‘System.Collections.Generic.IEnumerable’ does not
contain a definition for ‘event_id’ and no extension method ‘event_id’
accepting a first argument of type
‘System.Collections.Generic.IEnumerable’ could be found (are
you missing a using directive or an assembly reference?)
In my code I have :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using WebMatrix.Data;
What is causing this?
Well to start with, you probably meant
record.event_idinstead ofresult.event_id. Your code would be clearer if you changedresulttoresults, given that it’s the sequence of results.However, that’s just going to move the problem on one stage.
Database.Queryreturns anIEnumerable<object>, so the type ofrecordis going to beobject… andobjectdoesn’t have a member calledevent_ideither.The documentation for
Database.Queryis pretty vague – it’s possible that it’s returning dynamic objects though. Try this:EDIT: If
record.event_idis working even without converting todynamic, then it’s possible that the effective result type isIEnumerable<dynamic>and MSDN is just not being terribly helpful.However, there’s still a problem in your code: it’s a bad idea to include your parameter values directly in your SQL like that:
Instead, you should use a parameterized query like this: