That is because your _database
is derived from DbContext
and your AccountsOfMonth
is DbSet<>
. In such case you cannot use ObjectQuery
directly because DbSet<>
produces DbQuery<>
which is not convertible to ObjectQuery<>
.
You must either use DbQuery<>
directly:
var result = from acm in this._database.AccountsOnMonth
where ((acm.Year == year) && (acm.Month == month))
select acm.Id;
var query = (DbQuery<int>)result;
string sql = string.Format(
"DELETE FROM [AccountsOnMonth] WHERE [AccountsOnMonth].[Id] IN ({0})",
query.ToString()
);
Or you must first convert your context to ObjectContext
and create ObjectSet<>
:
var objectContext = ((IObjectContextAdapter)_database).ObjectContext;
var set = objectContext.CreateObjectSet<AccountsOnMonth>();
var resut = from acm in set
where ((acm.Year == year) && (acm.Month == month))
select acm.Id;
The problem with first approach is that DbQuery
doesn’t offer Parameters
collection – just another example of simplification in DbContext API which only makes it harder to use.