使用 Linq 表达式进行左外连接

发布于 2024-11-15 17:34:56 字数 3981 浏览 6 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

允世 2024-11-22 17:34:56

我更喜欢使用 MethodInfo 对 Expression.Call 进行不同的重载,这是我正在工作的一个简单示例。

Expression constant = Expression.Constant(new string[] { "a", "b" });
MethodInfo methodInfo = typeof(Enumerable).GetMethods().FirstOrDefault(c => (c as MethodInfo).Name == "DefaultIfEmpty");
methodInfo = methodInfo.MakeGenericMethod(typeof(string));

MethodCallExpression methodExpression = Expression.Call(methodInfo, constant);

I prefer a different overload for Expression.Call using MethodInfo, here is a simple example I have working.

Expression constant = Expression.Constant(new string[] { "a", "b" });
MethodInfo methodInfo = typeof(Enumerable).GetMethods().FirstOrDefault(c => (c as MethodInfo).Name == "DefaultIfEmpty");
methodInfo = methodInfo.MakeGenericMethod(typeof(string));

MethodCallExpression methodExpression = Expression.Call(methodInfo, constant);
北风几吹夏 2024-11-22 17:34:56

我定义了 Expression.Call 方法的错误类型参数。它不是 IQueryable,而只是 DataObjectField。这就是修复它的原因。

MethodCallExpression collectionCallExpression = 
    Expression.Call(
        typeof(Enumerable),
        "DefaultIfEmpty",
        new System.Type[]
        {
            typeof(DataObjectField)
        },
        Expression.Property(collectionParameter, newResultType.GetProperty("DataObjectFields"))

I had defined incorrect type parameter of the Expression.Call method. It wasn't IQueryable<DataObjectField> but only DataObjectField. This is what fixed it.

MethodCallExpression collectionCallExpression = 
    Expression.Call(
        typeof(Enumerable),
        "DefaultIfEmpty",
        new System.Type[]
        {
            typeof(DataObjectField)
        },
        Expression.Property(collectionParameter, newResultType.GetProperty("DataObjectFields"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文