Linq表达式的左外连接

时间:2011-06-17 15:57:39

标签: c# linq-to-sql linq-expressions linq

我正在尝试使用Linq Expressions构建左外连接查询,但现在我真的碰壁了。

我想要完成的是以下查询:

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表达式,它没有出现编译错误。 (我在这里遗漏了som代码):

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中的typeparams切换到IEnumerable,并且事件尝试调用Enumerable.DefaultIfEmpty而没有运气。也许这是PropertyExpression的错误?

2 个答案:

答案 0 :(得分:0)

我更喜欢使用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);

答案 1 :(得分:0)

我已经定义了Expression.Call方法的错误类型参数。它不是IQueryable<DataObjectField>而只是DataObjectField。这就是解决它的原因。

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