如何调整动态联接返回的结果
本文关键字:返回 结果 动态 何调整 调整 | 更新日期: 2023-09-27 18:31:14
public class Source
{
public int SourceID;
public int? SecurityId;
public int? CUSIP;
public string Text;
}
public class Destination
{
public int DestinationID;
public int? SecurityId;
public int? CUSIP;
public string Text;
}
IQueryable listleftJn =
listOfSources
.AsQueryable()
.GroupJoin(
listOfDestinations.AsQueryable(),
"new (outer.SecurityId as SecurityId, outer.CUSIP as CUSIP)",
"new (inner.SecurityId as SecurityId, inner.CUSIP as CUSIP)",
"new (outer as sources, group as destinations)")
.SelectMany(
"destinations",
"new(outer as sources, inner as destinations)");
如何塑造动态IQueryable listleftJn
的结果并将其映射到类类型。这样我就可以使用映射类供进一步使用。
我相信
你可以使用我对动态Linq的执行延迟IQueryable
首先,从链接添加相同的帮助程序方法
public static class DynamicQueryableEx
{
public static IQueryable<TResult> Select<TResult>(this IQueryable source, string selector, params object[] values)
{
if (source == null) throw new ArgumentNullException("source");
if (selector == null) throw new ArgumentNullException("selector");
var dynamicLambda = System.Linq.Dynamic.DynamicExpression.ParseLambda(source.ElementType, null, selector, values);
var memberInit = dynamicLambda.Body as MemberInitExpression;
if (memberInit == null) throw new NotSupportedException();
var resultType = typeof(TResult);
var bindings = memberInit.Bindings.Cast<MemberAssignment>()
.Select(mb => Expression.Bind(
(MemberInfo)resultType.GetProperty(mb.Member.Name) ?? resultType.GetField(mb.Member.Name),
mb.Expression));
var body = Expression.MemberInit(Expression.New(resultType), bindings);
var lambda = Expression.Lambda(body, dynamicLambda.Parameters);
return source.Provider.CreateQuery<TResult>(
Expression.Call(
typeof(Queryable), "Select",
new Type[] { source.ElementType, lambda.Body.Type },
source.Expression, Expression.Quote(lambda)));
}
}
然后你可以使用这样的东西
var result = listleftJn.Select<ResultOfSOurceAndDestination>(
"new (sources.SourceId as SourceId, destinations.DestinationID as DestinationID, sources.SecurityId as SecurityId, etc...)");
不要忘记使用 sources
和 destinations
访问器(您在 SelectMany
- "new(outer as sources, inner as destinations)"
中定义的)。我会在两个地方使用source
和destination
,或者更短的s
和d
,但一旦它们匹配,这不是必需的。
更新:您的SelectMany
投影中也存在问题。
"new(outer as sources, inner as destinations)"
应该是
"new(outer.sources as sources, inner as destinations)"