0

我正在将sql server查询转换为linq

 SELECT * FROM DataFlow where FlowDeleted = 0 and 
    DataFlow.FlowVersionNumber = 
    (
    SELECT MAX(FlowVersionNumber) from DataFlow i
    where FlowDeleted = 0 AND
    i.FlowCounter = DataFlow.FlowCounter
    group by FlowCounter
    )
    ORDER by 1

我的 Linq 查询代码是:

     public List<DataFlow> getdataflow() 
    var dflow = db.DataFlows.Where (
        d => d.FlowDeleted == false &&

        d.FlowVersionNumber =
        (
            db.DataFlows.Where(i => i.FlowDeleted == false && i.FlowCounter == d.FlowCounter).GroupBy(g => g.FlowCounter)
                .Select(s => s.Max(m => m.FlowVersionNumber))
        )
    )

.Select(s =>
    new DataFlow
    {
        FlowCounter = s.FlowCounter,
        FlowDescription = s.FlowDescription,
        FlowName = s.FlowName,
    }).OrderBy(o => o.FlowCounter);

    return dflow.ToList();
}

但它给出的错误如下:Operator '&&' cannot be applied to operands of type 'bool' ... 'string'"

请帮我

4

1 回答 1

1

您正在使用赋值运算符而不是比较...尝试..d.FlowVersionNumber ==...

db.DataFlows.Where(d => 
    d.FlowDeleted == false &&
    d.FlowVersionNumber == db.DataFlows.Where(i => 
        i.FlowDeleted == false && 
        i.FlowCounter == d.FlowCounter).GroupBy(g => g.FlowCounter)
                                       .First(s => s.Max(m => m.FlowVersionNumber))
    )
)

我改成SelectFirst因为你需要比较标量。但是,请确保您会找到一个项目,否则您可能会遇到异常。

于 2013-06-14T12:08:19.673 回答