There are some new operations in .NET 4 Entity Framework. This is a quick example of each working with VistaDB 4 and Visual Studio 2010. These changes were mostly made to bring LINQ to Entities inline with the other LINQ providers in .NET. Of the list below, I think that Single()
was the one that most people were confused about because if you used it, you would get weird errors that didn’t make a lot of sense.
For a complete list of LINQ to Entities operators, visit the Supported and Unsupported LINQ Methods on MSDN.
Entity Framework in .NET 4
The operations I want to demonstrate are the new Contains()
, Single()
, SingleOrDefault()
, and DefaultIfEmpty()
.
These operations are all new in .NET 4, and yes they work with VistaDB 4. I started with a simple one table database called Feedback
. The only table has 3 columns:
int FeedbackID
NText FeedbackText
DateTime FeedbackDate
I added a few small text entries, including “I like grapes
'” to search against with the Contains()
operator.
Adding the Entity Framework Model
I then added an Entity Framework to a simple command line application set with .NET 4 as the target in Visual Studio 2010.
The model is only that one table, no navigation or anything complicated.
This model was called FeedbackEntities
. Each model with Entity Framework requires you to allocate this Data Context object to perform queries against the database.
This sample model can be created using the following C# code:
using (FeedbackEntities fe = new FeedbackEntities())
{
}
Complete Entity Framework Sample Code
Here is the complete sample code for the operations against that database.
static void Main(string[] args)
{
using (FeedbackEntities fe = new FeedbackEntities())
{
var grapecomment = from f in fe.Feedbacks
where f.FeedbackText.Contains("grapes")
select f.FeedbackText;
Console.WriteLine( grapecomment.First() );
var oneresult = (
from f in fe.Feedbacks
where f.FeedbackText.Contains("GRAPES")
select f.FeedbackText
).Single();
Console.WriteLine(oneresult);
var singleordefault = (
from f in fe.Feedbacks
where f.FeedbackText.Contains("grapes")
select f.FeedbackText
).SingleOrDefault();
Console.WriteLine(singleordefault);
var notfound = (
from f in fe.Feedbacks
where f.FeedbackText.Contains("xxxxxxxx")
select f.FeedbackText
).DefaultIfEmpty("Not found");
Console.WriteLine(notfound.First());
}
}
Contains
Contains()
does a case insensitive compare for parts of the word in that field. You will notice that I had to do a .First()
on the grapecomment because the select
returns a collection, even if there is only one item in the collection.
Single
Single()
is used in the next example on the same query, but this time you only get back one item. This means you don’t need the .First()
from the first example.
SingleOrDefault
Returns back a single object, OR the default for that object. This means if it is a complex type, you could get back a null
. In this case, we get the same result.
DefaultIfEmpty
I changed this example to search for an item that does not exist on purpose. The DefaultIfEmpty()
will then return whatever you pass into it as the result. This means we will not find the result, and return “Not found
” as the string
rather than a string.Empty
. But note that this is now a collection again, so to print it, we have to take the First()
item of the collection.
Sample Output
This is what is returned by the above code:
I like grapes
I like grapes
I like grapes
Not found
Summary
There are a lot of operations you can perform through LINQ to Entities with an Entity Framework model. This is only a few of the new operations in .NET 4. See the Standard Query Operators Overview for more information.
I will put this sample up in the VistaDB 4 Samples Forum for those who want to download it and test it themselves.