This is a migrated thread and some comments may be shown as answers.

Extension on a Collection Property

1 Answer 54 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
sitefinitysteve asked on 21 Mar 2011, 03:59 PM
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>);

1 Answer, 1 is accepted

Sort by
0
Serge
Telerik team
answered on 23 Mar 2011, 04:04 PM
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
Tags
Development (API, general questions)
Asked by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Answers by
Serge
Telerik team
Share this question
or