How do I perform a distinct on a column I am selecting dynamically?
I am using dynamic linq and selecting a column dynamically. I need to do a distinct on this. How could I do this?
var qry =开发者_如何学C tbl.AsEnumerable().AsQueryable()
.Select("new(it[\"" + this.UniqueName + "\"]
.ToString() as " + this.UniqueName + ")");
Thanks.
Instead of using
as " + this.UniqueName + "
do
as someFixedColumnName
and run your Distinct()
clause on that, using ordinary Linq.
Alternatively, you could try this extension method:
public static IQueryable DynamicDistinct(this IQueryable source)
{
if (source == null) throw new ArgumentNullException("source");
return source.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Distinct",
new Type[] { source.ElementType },
source.Expression));
}
精彩评论