【问题标题】:How to frame Linq Query for this condition如何为这种情况构建 Linq 查询
【发布时间】:2012-10-16 09:06:29
【问题描述】:

我的表格文件和值:

IdFavorite:1,2,3,4,5

FavoriteName:Fav1、Fav2、Fav3、Fav4、Fav5

用户 ID:1、3、3、4、3

公众喜​​爱:0、1、0、1、0

截至目前,我使用以下 Linq 查询根据 用户 ID

获取 Favorite Names 列表
public IList<ReportFavorite> GetReportFavorites(int userId)
{
    return _reportFavoriteRepository.GetMany(x => x.UserId == userId).ToList();
}

现在,条件如上,我需要得到所有PublicFavorite为1的Favorite Names。我需要Linq 根据这个条件查询。谢谢。

例如:如果我的 User Id 为 3,那么我应该将 Favorite Names 设为 Fav2Fav3Fav5 以及 Fav4(因为 PublicFavorite 为 1)

【问题讨论】:

  • GetMany(x =&gt; x.UserId == userId || x.PublicFavorite == 1) -- 到底是什么困扰着你?
  • operator == 不能应用于 'bool' 类型的操作数,并显示 'int' msg。谢谢。

标签: c# asp.net linq c#-4.0 linq-to-sql


【解决方案1】:
var res =
    from item in _reportFavoriteRepository
    where item.UserId == userId || item.PublicFavorite == publicFavorite
    select item.FavoriteName;

【讨论】:

    【解决方案2】:

    我根据 Jon 的评论使用了以下代码。它按预期工作正常。

    GetMany(x => x.UserId == userId || x.PublicFavorite == 1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      相关资源
      最近更新 更多