0

I am attempting to use the answer in post: How do you sort an EntitySet<T> to expose an interface so that I can sort an EntitySet with a Binding list. I have created the class below and I get the following compiler error: "The type or namespace 'P' could not be found (are you missing a using directive or assembly reference?). Can someone tell me what the P means and which namespace I need to include to get the method below to compile? I am very new to delegates and lamba expressions.

Also, can someone confirm that if I create a BindingList from my EntitySet that any modifications I make to the BindingList will be made to the EntitySet?

Basically, I have an EntitySet that I need to sort and make changes to. Then, I will need to persist these changes using the original Entity that the BindingList came from.

public class EntitySetBindingWrapper<T> : BindingList<T>
{
    public EntitySetBindingWrapper(BindingList<T> root)
        : base(root)
    {
    }

            public void Sort<P>(Expression<Func<T, P>> expr, ListSortDirection direction)
    {
        if (expr == null)
            base.RemoveSortCore();

        MemberExpression propExpr = expr as MemberExpression;
        if (propExpr == null) throw new ArgumentException("You must provide a property", "expr");

        PropertyDescriptorCollection descriptorCol = TypeDescriptor.GetProperties(typeof(T));
        IEnumerable<PropertyDescriptor> descriptors = descriptorCol.Cast<PropertyDescriptor>();
        PropertyDescriptor descriptor = descriptors.First(pd => pd.Name == propExpr.Member.Name);

        base.ApplySortCore(descriptor, direction);
    }
}

I finally got the code above to compile but, now I am getting an error when I try to call the constructor:

The following code where currentPredefinedJob.fkItems is the EntitySet results in error: Cannot convert from System.ComponentModel.IBindingList to System.ComponentModel.BindingList

var bindingWrapper = new EntitySetBindingWrapper<PredefinedJobsItem>(currentPredefinedJob.fkItems.GetNewBindingList());

And, the following code results in error: Error 8 Using the generic type 'MarineService.Tests.EntitySetBindingWrapper' requires '1' type arguments

var bindingWrapper = new EntitySetBindingWrapper(currentPredefinedJob.fkItems.GetNewBindingList());

Can someone tell me how I need to call this constructor and confirm how I would sort the resulting BindingList?

Community
  • 1
  • 1
Grasshopper
  • 4,717
  • 9
  • 36
  • 62

2 Answers2

1

You need to specify the generic variable in either your class definition or method definition.

P will be the type returned by your function expr

public void Sort<P>(Expression<Func<T, P>> expr, ListSortDirection direction)
Aducci
  • 26,101
  • 8
  • 63
  • 67
  • Okay, I think I understand the part about P being the type returned. But, I cannot even get the class to compile with P in the method signature. Is there a namespace that I am missing? So far, I have included: System.Linq.Expressions, System.Linq.Data, and System.Linq. – Grasshopper Aug 31 '12 at 15:43
  • Once I cleaned the project and recompiled, I was able to resolve that error but, now I am getting another once when I try to instantiate the list. Would you mind taking a look at my code samples and corresponding errors in the modified post? – Grasshopper Aug 31 '12 at 16:22
  • @Grasshopper - You haven't modified the method definition to include `P`? – Aducci Aug 31 '12 at 16:25
  • I did modify the method but, now I cannot figure out the right syntax to call the constructor. – Grasshopper Aug 31 '12 at 16:50
  • @Grasshopper - If you updated your code, you should update your question here with the correct method definition – Aducci Aug 31 '12 at 16:53
  • I just updated the Sort method signature in posting. The constructor is the same which is where I'm having issues. – Grasshopper Aug 31 '12 at 17:27
0

For calling the constructor, the following works fine:

var w = new EntitySetBindingWrapper<String>(new System.ComponentModel.BindingList<string>());

Is it possible the problem is coming in what you are doing inside of currentPredefinedJob.fkItems.GetNewBindingList()?

Jim Wooley
  • 10,169
  • 1
  • 25
  • 43