Pull transactions by Date in Entity Framework
In order to process our daily transactions on the back office server, I need to pull the transactions from the web server's database. What is the recommended approach to pull all records for a given date in a DateTime field on the SQL Server?
var transactions = from i in db.Transactions
where i.TransactionTimestamp.Year == transactionDate.Y开发者_开发问答ear &&
i.TransactionTimestamp.Month == transactionDate.Month &&
i.TransactionTimestamp.Day == transactionDate.Day
select new Transaction();
I got the following error when i tried the above:
System.NotSupportedException was unhandled
Message=The entity or complex type 'db_eGovModel.Transaction' cannot be constructed in a LINQ to Entities query.
You currently are querying for transactions, then throwing the result away and create a new Transaction
instance from scratch - this is not what you want, also it will cause a runtime error since there is no equivalent of a transaction instance on the SQL side.
Instead just return the entities you queried for - Try this:
var transactions = from i in db.Transactions
where i.TransactionTimestamp.Year == transactionDate.Year &&
i.TransactionTimestamp.Month == transactionDate.Month &&
i.TransactionTimestamp.Day == transactionDate.Day
select i;
You might be able to use this?
var transactions = from i in db.Transactions
where i.TransactionTimestamp == EntityFunctions.TruncateTime(transactionDate)
select i;
精彩评论