locked
Where is the implementation of extension methods in Linq RRS feed

  • Question

  • User-1559607405 posted

    I don't under stand where is the implementation or how it is works look for example at OrderBy Method?

    namespace System.Linq
    {
        // Summary:
        //     Provides a set of static (Shared in Visual Basic) methods for querying data
        //     structures that implement System.Linq.IQueryable<T>.
        public static class Queryable
        {
    
     public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector);
    
    
      //
            // Summary:
            //     Sorts the elements of a sequence in ascending order by using a specified
            //     comparer.
            //
            // Parameters:
            //   source:
            //     A sequence of values to order.
            //
            //   keySelector:
            //     A function to extract a key from an element.
            //
            //   comparer:
            //     An System.Collections.Generic.IComparer<T> to compare keys.
            //
            // Type parameters:
            //   TSource:
            //     The type of the elements of source.
            //
            //   TKey:
            //     The type of the key returned by the function that is represented by keySelector.
            //
            // Returns:
            //     An System.Linq.IOrderedQueryable<T> whose elements are sorted according to
            //     a key.
            //
            // Exceptions:
            //   System.ArgumentNullException:
            //     source or keySelector or comparer is null.


    he implementation of extension methods for public static class Queryable

    Sunday, March 22, 2015 2:45 PM

Answers

  • User753101303 posted

    Hi,

    You are looking at this where? EF is open source but I suspect you are user the object browser that just show the class definition?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 3, 2015 11:41 AM

All replies

  • User-271186128 posted

    Hi Malik,

    From your description, I suppose you want to add Custom Methods for LINQ Queries. If that is the case, you could refer to the following code:

    public static class LINQExtension
    {
        public static double Median(this IEnumerable<double> source)
        {
            if (source.Count() == 0)
            {
                throw new InvalidOperationException("Cannot compute median for an empty set.");
            }
    
            var sortedList = from number in source
                             orderby number
                             select number;
    
            int itemIndex = (int)sortedList.Count() / 2;
    
            if (sortedList.Count() % 2 == 0)
            {
                // Even number of items. 
                return (sortedList.ElementAt(itemIndex) + sortedList.ElementAt(itemIndex - 1)) / 2;
            }
            else
            {
                // Odd number of items. 
                return sortedList.ElementAt(itemIndex);
            }
        }
    }
    
            double[] numbers1 = { 1.9, 2, 8, 4, 5.7, 6, 7.2, 0 };
            var query1 = numbers1.Median();
            Console.WriteLine("double: Median = " + query1);
    /*
     This code produces the following output:
    
     Double: Median = 4.85
    */

    For more details, please see: https://msdn.microsoft.com/en-us/library/cc981895.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

    Best Regards,
    Dillion

    Monday, March 23, 2015 7:51 AM
  • User-1559607405 posted

    Thank you Zhi Lv

    I 'am already know the methods extension in C# but my question was look at Querable class  OrderBy Method , where is the implementation of this method ?

    Monday, March 23, 2015 12:30 PM
  • User753101303 posted

    Hi,

    You are looking at this where? EF is open source but I suspect you are user the object browser that just show the class definition?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 3, 2015 11:41 AM