March 22, 2008  

At my last talk I had the following question.

"When returning a LINQ entity using the .Single() extension, what would happen if the result returned more than one row?"

The simple answer of course was that it would throw an exception, but I told him I would elaborate in a blog post. So here it is. Most LINQ queries will return an IEnumerable collection of objects as the result of your LINQ query. In order to only return a single entity, you have a couple of options.

 

Single – Will return back a single entity
SingleOrDefault – Will return back a single entity or a Default if nothing is found

Both of these will throw an InvalidOperationException if the query returns more than one row.

If you know that your query may return more than one record, you can use the following Extension methods to return a single entity from a collection of entities. (or the OrDefault versions)

ElementAt - return the entity at the given index

First - Returns the first entity of the collection

Last – Returns the last entity in the collection.

Finally, you can also pass a predicate (a condition) as a parameter.

Here we are looking for the first publisher in the pubisher collecdtion that has a country of USA.

 

Hope that helps.

Happy Programming

Doc


 

|Categories :   Comments [0] Trackback

So during my last LINQ talk, I had a couple of people ask me why Partial Method were both implicitly private and must return void. So as to not slow down the presentation (we had a lot to cover) I told them I would blog an answer for them. Here it is.

So first, let's set up our sample program. We created two partial classes. One with the partial methods implemented.

namespace CSharpBlogSample

{

internal partial class Account

{

//Partial Methods (NOT IMPLEMENTED)

partial void OnWithdraw();

partial void OnDeposit();

}

 

internal partial class Account

{

//Partial Method (IMPLEMENTED)

partial void OnWithdraw()

{

Console.WriteLine("Withdrawing Money...");

}

partial void OnDeposit()

{

Console.WriteLine("Depositing Money.");

}

 

}

}

If we dig down into the IL we will see the following.

Notice how the OnDeposit and OnWithdraw methods are compiled into the dll.

If we compare that with a implementation where we do not implement the partial methods you will notice that the methods will be missing from the IL code.

namespace CSharpBlogSample

{

internal partial class Account

{

//Partial Methods (NOT IMPLEMENTED)

partial void OnWithdraw();

partial void OnDeposit();

 

}

 

 

}

This is important; if these partial methods were not implicitly private and you tried to access them from another class, you would run into trouble when they don't exist.

This is the same reason that we also need to return void.

Let's say that we wanted to call our private methods from inside our class in a "CallMyMethods" function (shown below)

internal partial class Account

{

//Partial Methods (NOT IMPLEMENTED)

partial void OnWithdraw();

partial void OnDeposit();

 

void CallMyMethods()

{

 

OnDeposit();

OnWithdraw();

 

}

}

 

Since we are not implementing them in a separate partial class the methods will not be implemented inside of the CallMyMetods function.

 

If you were allow to return something, and were in turn using that as a return value from your CallMyMethods fuction, you would run into trouble when the partial methods were not implemented.

 

Hope that helps.

Happy Programming

Docwww.

 

|Categories :   Comments [0] Trackback