【问题标题】:Iinq equivalent to stored procedureIinq 等价于存储过程
【发布时间】:2013-08-25 07:14:48
【问题描述】:

我正在尝试编写与我拥有的存储过程等效的 linq 语句。我所拥有的没有返回任何结果,所以我认为某处一定是错误的。

我目前拥有的东西

from r in context.View
where ValOne== null ? false : r.ColOne.Equals(ValOne) && 
      ValTwo == null ? false : r.ColTwo.Equals(ValTwo) && 
      (r.ODate >= Start && r.ODate <= End) 
select r

我要改成linq的东西

select * from View
Where (@ValOne is null or ColOne = @ValOne)
  and (@ValTwo is null or ColTwo = @ValTwo)
  and (@Start is null or ODate between @Start and @End)

【问题讨论】:

    标签: c# sql linq stored-procedures


    【解决方案1】:

    试试

       from r in context.View
       where (ValOne == null || r.ColOne.Equals(ValOne)) && 
          (ValTwo == null ||r.ColTwo.Equals(ValTwo)) && 
          (Start == null || (r.ODate >= Start && r.ODate <= End)) 
       select r
    

    【讨论】:

      【解决方案2】:

      您的 sql 查询返回 ValOne 为空、ValTwo 为空且 Start 也为空的行。但是您的 linq 查询检查 ValOne 和 ValTwo 是否为空,但如果是这种情况,则分配 false ,这不能反映您的 sql 查询正在做什么。

      你可以试试:

      from r in context.View
      where ValOne== null ? true: r.ColOne.Equals(ValOne) 
          && ValTwo == null ? true: r.ColTwo.Equals(ValTwo) 
          && (r.ODate >= Start && r.ODate <= End) 
      select r
      

      【讨论】:

        【解决方案3】:
        from r in context.View
        where ValOne== null || r.ColOne.Equals(ValOne) && 
              ValTwo == null || r.ColTwo.Equals(ValTwo) && 
              (r.ODate >= Start || r.ODate <= End) 
        select r
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-07
          • 2012-04-17
          • 1970-01-01
          相关资源
          最近更新 更多