Skip to content
Skip to content
sujatad.net

Blogging about technology and leadership

← Visual Studio 2008 and .NET Framework 3.5 Service Pack 1 Beta
Learning LINQ – skip →
-->

Learning LINQ – Extension methods

Posted on May 20, 2008 by sujatad

Extension methods can be used to extend the set of methods that you can use for LINQ queries,

namespace TestConsoleApplication
{
class Program
{
static void Main(string[] args)
{
int[] myNumbers = { 1, 2, 3, 4, 5,7 };
// the j=>j lambda expression as a parameter to the method so compiler
// implicitly converts its value to double.
var avgQuery = myNumbers.MyCustomAverage(j => j);
Console.WriteLine(“My Average %=” + avgQuery);
}
}
//LINQ extension class
public static class MyLINQExtension
{
//This method takes – Func delegate as a parameter. This delegate // takes an object of generic type T and returns an object of type double.
public static double MyCustomAverage(this IEnumerable numbers, Func selector)
{
return ((from i in numbers select selector(i)).Average()/100);
}
}
}

About sujatad

Technical Blogger
View all posts by sujatad
This entry was posted in .NET, Entity Framework, LINQ, Microsoft. Bookmark the <a href="https://sujatad.net/net/learning-linq-extension-methods/" title="Permalink to Learning LINQ – Extension methods" rel="bookmark">permalink</a>.
← Visual Studio 2008 and .NET Framework 3.5 Service Pack 1 Beta
Learning LINQ – skip →

Comments are closed.

© 2025 | Blog info WordPress Theme | By Bharat Kambariya