【问题标题】:EF: Select columns where any of them has value but not all of themEF:选择其中任何一个具有价值但不是全部的列
【发布时间】:2014-03-20 14:09:09
【问题描述】:

以下是我根据以下条件检索一些数据的代码:

allMembers.Where(Function(x) Not x.firstDate Is Nothing Or
                             Not x.secondDate Is Nothing Or
                             Not x.thirdDate Is Nothing).ToList()

这将为我带来任何列具有值的数据(非空)

我的问题是,此代码还将获取所有三列都有值的记录。我需要过滤那些所有三列都有值的记录,并获取三列中任何一个都有值的记录。

有什么想法吗?

【问题讨论】:

  • 没有得到你需要那些三个记录都没有的记录吗?
  • 不,我想获取三列中的任何一列都具有值但不是同时具有所有三列的记录。

标签: sql vb.net linq entity-framework


【解决方案1】:
    allMembers.Where(Function(x)  Criteria(x)).ToList()


function Criteria (x as YourType) as boolean
 dim test as int =IF(x.firstDate Is Nothing, 1, 0) +
                  IF(x.secondDate Is Nothing, 1, 0) +
                  IF(x.thirdDate Is Nothing, 1, 0)

 return test > 0 and test < 3

end function

【讨论】:

  • allMembers 是一个列表(属于 myEntity)。有没有更简单的方法来过滤那些包含所有三个记录的记录?
  • 顺便说一句,这行得通。尽管该解决方案似乎不是可以直接在 linq 查询本身中解决的正确和最有效的方法,但我确实喜欢使用函数的想法,因为这是我第一次看到这样的事情并且可以想象我看到自己使用类似方法的很多场景。因此,我接受答案。
猜你喜欢
  • 1970-01-01
  • 2017-02-14
  • 2015-07-17
  • 2019-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
相关资源
最近更新 更多