Reputation: 529
I have the following property class:
class tbl_tax_type
{
public int TAXT_ID { get; set; }
public string BFNS_CODE { get; set; }
public string TAXT_CODE { get; set; }
public string TAXT_DESC { get; set; }
public string ACCT_CODE { get; set; }
public DateTime CREATED_DATE { get; set; }
public string CREATED_BY { get; set; }
public Nullable<DateTime> LAST_MODIFIED_DATE { get; set; }
public string LAST_MODIFIED_BY { get; set; }
public string TAXT_STATUS { get; set; }
}
I use the method below to retrieve data from it:
private static List<T> SelectData<T>(Expression<Func<T, bool>> PredicateExp) where T : new()
{
return FireCon().Table<T>().Where(PredicateExp).ToList();
}
By Usage (Code below selects rows from tbl_tax_type where BFNS_CODE is equal to 0605)
List<tbl_tax_type> TempList = SelectData<Tbl.tbl_tax_type>(taxtype => taxtype.BFNS_CODE == "0605")
Now I want to apply distinction on the selected data, so I came up to this method.
public static List<T> DistinctBy<T>(List<T> MyList, Func<T, __> SelectorExpr)
{
return GenericList.Select(SelectorExpr).Distinct().ToList();
}
By Usage (Code below selects distinct TAXT_CODE)
DistinctBy(TempList, v => v.TAXT_CODE);
There's something missing, the TResult of the selector expression. I want it to return any type of data because the properties of tbl_tax_type had different data types. Is this possible?
Upvotes: 0
Views: 104
Reputation: 117055
Try this:
public static List<T> DistinctBy<T, S>(List<T> myList, Func<T, S> selectorExpr)
{
return myList.ToLookup(selectorExpr).SelectMany(t => t.Take(1)).ToList();
}
Upvotes: 1
Reputation: 6864
You mean something like ...
public static List<S> DistinctBy<T, S>(List<T> myList, Func<T, S> selectorExpr)
{
return myList.Select(selectorExpr).Distinct().ToList();
}
?
Upvotes: 1