how to parameterize a select distinct query?

Your first example turned each instance of Model into an anonymous type, then later back into a Model again. Instead, the generic distinct method should create a new Model with only the requested fields.

public void getDistinct<TOrder>(ObservableCollection<Model> FilteredData, Func<Model, Model> distinct, Func<Model, TOrder> sort)
    {
        var distinct = FilteredData.Select(distinct).Distinct().OrderBy(sort).ToList();

        DistinctCustomerCodeList.Clear();
        DistinctCustomerCodeList.AddRange(distinct);
    }

Then this should work, with the compiler able to infer the generic types;

getDistinct(FilteredData, i => new Model{ i.CustomerCode, i.FamilyName }, x => x.FamilyName);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top