Extension on a Collection Property

2 posts, 0 answers
  1. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 21 Mar 2011 Link to this post

    I have a ton of extensions to extend the collections in the base context
    ex
    /// <summary>
    /// Gets a single submission entry by ID
    /// </summary>
    /// <param name="submissionID">Id of the submission</param>
    /// <returns>Deli.Forms.Model.Submission</returns>
    public static Deli.Forms.Model.Submission Get(this IQueryable<Deli.Forms.Model.Submission> submissions, Guid submissionID) {
        return submissions.FirstOrDefault(x => x.SubmissionID == submissionID);
    }
     (works great)

    However, how do I write an extension on a Navigation Collection Property of a specific object.

    So Submission has a collection called SubmissionResponses.  I want to have an extension work like this
    submission.SubmissionResponses.Get(<guid>);

    Instead of what I can do now which is
    submission.GetResponse(<guid>);

  2. Serge
    Admin
    Serge avatar
    375 posts

    Posted 23 Mar 2011 Link to this post

    Hi Steve,

     You could easily achieve the same setup with the collection property of a retrieved object using an extension method like : 

    public static class Extensions
    {
        public static Product Get(this IList<Product> list, int productID)
        {
            return list.FirstOrDefault(x => int.Equals(x.ProductID, productID));
        }
    }

    I have prepared and attached a sample project that has reverse mapped the standard Northwind database. 

    I hope this is helpful. 


    Kind regards,
    Serge
    the Telerik team
  3. DevCraft banner
Back to Top