Domanda

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?

È stato utile?

Soluzione

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)

Altri suggerimenti

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()?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top