开发者

How do I perform a distinct on a column I am selecting dynamically?

开发者 https://www.devze.com 2023-02-24 21:04 出处:网络
I am using dynamic linq and selecting a column dynamically. I need to do a distinct on this. How could I do this?

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

精彩评论

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