- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 System.Linq.Dynamic 中,有几种方法可以动态形成 Select、Where 和其他 Linq 语句。但是 SelectMany 没有。
Select的方法如下:
public static IQueryable Select(this IQueryable source, string selector, params object[] values)
{
if (source == null) throw new ArgumentNullException("source");
if (selector == null) throw new ArgumentNullException("selector");
LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, null, selector, values);
IQueryable result = source.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Select",
new Type[] { source.ElementType, lambda.Body.Type },
source.Expression, Expression.Quote(lambda)));
return result;
}
我试图修改上面的代码,经过几个小时的工作,我找不到出路。
欢迎提出任何建议。
莹
最佳答案
已经为我们的项目实现了这个,让我知道它是否适合你!
public static IQueryable SelectMany(this IQueryable source, string selector, params object[] values)
{
if (source == null)
throw new ArgumentNullException("source");
if (selector == null)
throw new ArgumentNullException("selector");
// Parse the lambda
LambdaExpression lambda =
DynamicExpression.ParseLambda(source.ElementType, null, selector, values);
// Fix lambda by recreating to be of correct Func<> type in case
// the expression parsed to something other than IEnumerable<T>.
// For instance, a expression evaluating to List<T> would result
// in a lambda of type Func<T, List<T>> when we need one of type
// an Func<T, IEnumerable<T> in order to call SelectMany().
Type inputType = source.Expression.Type.GetGenericArguments()[0];
Type resultType = lambda.Body.Type.GetGenericArguments()[0];
Type enumerableType = typeof(IEnumerable<>).MakeGenericType(resultType);
Type delegateType = typeof(Func<,>).MakeGenericType(inputType, enumerableType);
lambda = Expression.Lambda(delegateType, lambda.Body, lambda.Parameters);
// Create the new query
return source.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "SelectMany",
new Type[] { source.ElementType, resultType },
source.Expression, Expression.Quote(lambda)));
}
关于dynamicquery - 以 System.Linq.Dynamic 的方式动态调用 SelectMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2983134/
我有一个 SQL 查询: Select t1.* From tracking As t1 Inner Join ( Select Max(trackingid) As trackingid,
我在 Liferay 6 中进行动态查询时遇到问题。我正在尝试对订单 JournalArticles 进行查询基于他们的view count 。 View 计数在另一个表 ( AssetEntry )
在 System.Linq.Dynamic 中,有几种方法可以动态形成 Select、Where 和其他 Linq 语句。但是 SelectMany 没有。 Select的方法如下: publ
我想在 liferay 7.1 中对具有别名的 MySQL 数据库执行查询,所以我更喜欢动态查询的方式 我的查询: 从 LRCXCHTASK.JournalArticle 中选择 id_、groupI
我的代码中有以下查询: DynamicQuery journalArticleDynamicQuery = JournalArticleLocalServiceUtil.dynamicQuery();
我是一名优秀的程序员,十分优秀!