简体   繁体   English

如何在实体框架中将SQL Server查询转换为LINQ

[英]How to Convert sql server query to linq in entity framework

I am converting the sql server query to linq 我正在将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

My Linq query Code is: 我的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();
}

But it giving error like: Operator '&&' cannot be applied to operands of type 'bool' ... 'string'" 但是它给出如下错误: Operator '&&' cannot be applied to operands of type 'bool' ... 'string'"

Please help me 请帮我

You are using assignment operator instead of comparing... try ..d.FlowVersionNumber ==... 您正在使用赋值运算符而不是进行比较...试试..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))
    )
)

I changed Select to First because you need to compare scalars. 我将“ Select更改为“ First因为您需要比较标量。 However, be sure that you WILL find an item, or you could get an exception. 但是,请确保您将找到一个项目,否则可能会遇到异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM