使用 Linq 表达式进行左外连接
我正在尝试使用 Linq 表达式构建左外连接查询,但现在我真的遇到了困难。
我想要完成的是以下查询:
var q =
from i in ProcessInstances
join dof1 in Queries.DataObjectFieldsQuery(this) on new { instanceId = i.ObjectID, dataId = fields[0,0], fieldId = fields[0,1] } equals new { instanceId = dof1.ProcessInstanceObjectID, dataId = dof1.DataID, fieldId = dof1.FieldID } into dofs1
from dof1 in dofs1.DefaultIfEmpty()
join dof2 in Queries.DataObjectFieldsQuery(this) on new { instanceId = i.ObjectID, dataId = fields[1,0], fieldId = fields[1,1] } equals new { instanceId = dof2.ProcessInstanceObjectID, dataId = dof2.DataID, fieldId = dof2.FieldID } into dofs2
from dof2 in dofs2.DefaultIfEmpty()
join dof3 in Queries.DataObjectFieldsQuery(this) on new { instanceId = i.ObjectID, dataId = fields[2,0], fieldId = fields[2,1] } equals new { instanceId = dof3.ProcessInstanceObjectID, dataId = dof3.DataID, fieldId = dof3.FieldID } into dofs3
from dof3 in dofs3.DefaultIfEmpty()
select new WorkitemListModel
{
InstanceId = i.ObjectID,
FormFieldValue1 = dof1.FieldValue,
FormFieldValue2 = dof2.FieldValue,
FormFieldValue3 = dof3.FieldValue,
};
我定义了以下类:
public class WorkitemListModel
{
public string InstanceId { get; set; }
public string FormFieldValue1 { get; set; }
public string FormFieldValue2 { get; set; }
public string FormFieldValue3 { get; set; }
}
public class DataObjectField
{
public string ProcessInstanceObjectID { get; set; }
public string DataID { get; set; }
public string FieldID { get; set; }
public string FieldValue { get; set; }
}
public class ModelWithFields<TModel>
{
public IEnumerable<DataObjectField> DataObjectFields { get; set; }
public TModel Model { get; set; }
}
public class OuterKeySelector
{
public string instanceId { get; set; }
public string dataId { get; set; }
public string fieldId { get; set; }
}
我创建了 GroupJoin 表达式,没有给出任何编译错误。 (我在这里遗漏了一些代码):
var q = dc.Instances;
System.Type modelType = typeof(ModelWithFields<>);
System.Type outerKeyType = typeof(WorkitemListModel);
System.Type resultType = modelType.MakeGenericType(outerKeyType);
//... MemberInitExpression and Expression.Bind
q = q.Provider.CreateQuery(
Expression.Call(
typeof(Queryable),
"GroupJoin",
new[]
{
typeof(WorkitemListModel),
typeof(DataObjectField),
typeof(OuterKeySelector),
resultType,
},
query.Expression,
Expression.Constant(Queries.DataObjectFieldsQuery(dc)),
Expression.Quote(outerLambda),
Expression.Quote((Expression<Func<DataObjectField,OuterKeySelector>>)(
(DataObjectField dof) =>
new OuterKeySelector
{
instanceId = dof.ProcessInstanceObjectID,
dataId = dof.DataID,
fieldId = dof.FieldID
})),
Expression.Quote(resultLambda)));
// selectmany expression
// collectionSelector lambda -- temp.DataObjectFields.DefaultIfEmpty()
ParameterExpression collectionParameter = Expression.Parameter(resultType, "temp");
// This throw an exception
MethodCallExpression collectionCallExpression =
Expression.Call(
typeof(Queryable),
"DefaultIfEmpty",
new System.Type[]
{
typeof(IQueryable<>).MakeGenericType(typeof(DataObjectField))
},
Expression.Property(collectionParameter, resultType.GetProperty("DataObjectFields")));
但是在 SelectMany 方法中,我尝试添加 DefaultIfEmpty 但出现异常:
没有泛型方法“DefaultIfEmpty” 类型“System.Linq.Queryable”是 与提供的类型兼容 论据和论据。无类型 如果 方法是非泛型的。
我尝试将类型参数从 IQueryable 切换到 IEnumerable,并且事件尝试调用 Enumerable.DefaultIfEmpty 但没有成功。也许 PropertyExpression 有问题?
I'm trying to build left outer join queries with Linq Expressions but now I really hit the wall.
What I want to accomplish is the following query:
var q =
from i in ProcessInstances
join dof1 in Queries.DataObjectFieldsQuery(this) on new { instanceId = i.ObjectID, dataId = fields[0,0], fieldId = fields[0,1] } equals new { instanceId = dof1.ProcessInstanceObjectID, dataId = dof1.DataID, fieldId = dof1.FieldID } into dofs1
from dof1 in dofs1.DefaultIfEmpty()
join dof2 in Queries.DataObjectFieldsQuery(this) on new { instanceId = i.ObjectID, dataId = fields[1,0], fieldId = fields[1,1] } equals new { instanceId = dof2.ProcessInstanceObjectID, dataId = dof2.DataID, fieldId = dof2.FieldID } into dofs2
from dof2 in dofs2.DefaultIfEmpty()
join dof3 in Queries.DataObjectFieldsQuery(this) on new { instanceId = i.ObjectID, dataId = fields[2,0], fieldId = fields[2,1] } equals new { instanceId = dof3.ProcessInstanceObjectID, dataId = dof3.DataID, fieldId = dof3.FieldID } into dofs3
from dof3 in dofs3.DefaultIfEmpty()
select new WorkitemListModel
{
InstanceId = i.ObjectID,
FormFieldValue1 = dof1.FieldValue,
FormFieldValue2 = dof2.FieldValue,
FormFieldValue3 = dof3.FieldValue,
};
I have defined the following classes:
public class WorkitemListModel
{
public string InstanceId { get; set; }
public string FormFieldValue1 { get; set; }
public string FormFieldValue2 { get; set; }
public string FormFieldValue3 { get; set; }
}
public class DataObjectField
{
public string ProcessInstanceObjectID { get; set; }
public string DataID { get; set; }
public string FieldID { get; set; }
public string FieldValue { get; set; }
}
public class ModelWithFields<TModel>
{
public IEnumerable<DataObjectField> DataObjectFields { get; set; }
public TModel Model { get; set; }
}
public class OuterKeySelector
{
public string instanceId { get; set; }
public string dataId { get; set; }
public string fieldId { get; set; }
}
I created the GroupJoin expression wich gives no compile errors. (I left out som code here):
var q = dc.Instances;
System.Type modelType = typeof(ModelWithFields<>);
System.Type outerKeyType = typeof(WorkitemListModel);
System.Type resultType = modelType.MakeGenericType(outerKeyType);
//... MemberInitExpression and Expression.Bind
q = q.Provider.CreateQuery(
Expression.Call(
typeof(Queryable),
"GroupJoin",
new[]
{
typeof(WorkitemListModel),
typeof(DataObjectField),
typeof(OuterKeySelector),
resultType,
},
query.Expression,
Expression.Constant(Queries.DataObjectFieldsQuery(dc)),
Expression.Quote(outerLambda),
Expression.Quote((Expression<Func<DataObjectField,OuterKeySelector>>)(
(DataObjectField dof) =>
new OuterKeySelector
{
instanceId = dof.ProcessInstanceObjectID,
dataId = dof.DataID,
fieldId = dof.FieldID
})),
Expression.Quote(resultLambda)));
// selectmany expression
// collectionSelector lambda -- temp.DataObjectFields.DefaultIfEmpty()
ParameterExpression collectionParameter = Expression.Parameter(resultType, "temp");
// This throw an exception
MethodCallExpression collectionCallExpression =
Expression.Call(
typeof(Queryable),
"DefaultIfEmpty",
new System.Type[]
{
typeof(IQueryable<>).MakeGenericType(typeof(DataObjectField))
},
Expression.Property(collectionParameter, resultType.GetProperty("DataObjectFields")));
But in the SelectMany method I'm trying to add DefaultIfEmpty but I get an exception saying:
No generic method 'DefaultIfEmpty' on
type 'System.Linq.Queryable' is
compatible with the supplied type
arguments and arguments. No type
arguments should be provided if the
method is non-generic.
I've tried the switched the typeparams from IQueryable to IEnumerable and event tried to call Enumerable.DefaultIfEmpty with no luck. Perhaps it's something wrong with the PropertyExpression?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我更喜欢使用
MethodInfo
对 Expression.Call 进行不同的重载,这是我正在工作的一个简单示例。I prefer a different overload for Expression.Call using
MethodInfo
, here is a simple example I have working.我定义了 Expression.Call 方法的错误类型参数。它不是
IQueryable
,而只是DataObjectField
。这就是修复它的原因。I had defined incorrect type parameter of the Expression.Call method. It wasn't
IQueryable<DataObjectField>
but onlyDataObjectField
. This is what fixed it.