3

我正在使用动态 linq 并动态选择一列。我需要对此做一个明确的。我怎么能这样做?

var qry = tbl.AsEnumerable().AsQueryable()
             .Select("new(it[\"" + this.UniqueName + "\"]
             .ToString() as " + this.UniqueName + ")");

谢谢。

4

1 回答 1

4

而不是使用

as " + this.UniqueName + "

as someFixedColumnName

并使用普通的 Linq 运行您的Distinct()子句。


或者,您可以尝试这种扩展方法:

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));
}
于 2011-04-13T20:47:17.723 回答