开发者

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));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜