【问题标题】:check whether LINQ query returns rows检查 LINQ 查询是否返回行
【发布时间】:2014-02-15 17:28:00
【问题描述】:

我需要检查查询是否返回行,如果返回,则将其更改为字符串,但如果没有,则返回“In Progress”。我认为下面的代码会起作用,但这总是正确的:

if (System.Linq.Enumerable.Count(columns) == 0)<--- always true but it shouldn't be

当没有行返回到列时,我的 jQuery Ajax 中出现以下错误: “转换为值类型 \u0027Int32\u0027 失败,因为具体化值为 null。结果类型\u0027s 泛型参数或查询必须使用可为空的类型。”

这是我的 WebMethod:

using (dbPSREntities5 myEntities = new dbPSREntities5())
    {
        var thisId = myEntities.tbBreadCrumbs.Where(x => x.ProjectID == projectID && x.StatusID == statusID).Max(x => x.BreadCrumbID);
        var columns = myEntities.tbBreadCrumbs
            .Where(x => x.BreadCrumbID == thisId)
            .Select(x => x.CreateDateTime)
            .ToList();

        if (System.Linq.Enumerable.Count(columns) == 0)
        {
            var formattedList = columns
                .Select(d => null != d
                    ? d.Value.ToString("MMM dd, yyyy")
                    : string.Empty) // this is just one example to handle null
                .ToList();

            return formattedList;<-- return this if there is a BreadCrumbID (columns would have a row)
        }
        else
        {
            return "In Progress";<--- there was no BreadCrumbID (columns would have 0 rows)
        }

    }

【问题讨论】:

  • 你试过if(!columns.Any()) { ... }
  • 其实正如别人指出的,他的情况是倒退的。应该是 columns.Any(),你第一次做对了 :)

标签: c# linq webmethod


【解决方案1】:

你可以使用Any()

MSDN Enumerable.Any Method

【讨论】:

    【解决方案2】:

    你的条件不对。计数必须大于零

    【讨论】:

      【解决方案3】:

      您只需检查.Count() &gt; 0

      if (columns.Count() > 0)
              {
                  var formattedList = columns
                      .Select(d => null != d
                          ? d.Value.ToString("MMM dd, yyyy")
                          : string.Empty) // this is just one example to handle null
                      .ToList();
      
                  return formattedList;<-- return this if there is a BreadCrumbID (columns would have a row)
              }
              else
              {
                  return "In Progress";<--- there was no BreadCrumbID (columns would have 0 rows)
              }
      

      【讨论】:

        【解决方案4】:

        使用List&lt;T&gt;提供的.Any()方法:

        If (columns.Any())
        {
            // do your bidding
        } else {
            // in progress code
        }
        

        您的方法还有两个不同的返回签名。那不会编译。不能返回 List 或字符串,除非返回类型是 object(不推荐)。

        我建议返回一个空列表并检查它在你的 UI 层中是否为空,然后显示适当的字符串或日期值,因为它们最终在 UI 中都是字符串。

        【讨论】:

          【解决方案5】:

          你首先检查Count == 0,听起来不对,我猜你需要相反的检查。您应该使用AnyCount() &gt; 0 检查,例如:

          if (columns.Any()) //Or columns.Count() > 0
          {
              var formattedList = columns
                  .Select(d => null != d
                      ? d.Value.ToString("MMM dd, yyyy")
                      : string.Empty) // this is just one example to handle null
                  .ToList();
          
              return formattedList;<-- return this if there is a BreadCrumbID (columns would have a row)
          }
          else
          {
              return "In Progress";<--- there was no BreadCrumbID (columns would have 0 rows)
          }
          

          您必须将您的列表转换为字符串,以便您的方法返回一个字符串。

          【讨论】:

          • 这很好用。谢谢。但我仍然收到错误消息:“转换为值类型 \u0027Int32\u0027 失败,因为具体化值为 null。结果类型\u0027s 泛型参数或查询必须使用可为空的类型。”我认为这是因为 thisId 不返回任何行。我应该提交一个新问题,还是你知道我如何有条件地检查 thisID 是否为 NULL。
          • @user1431633,当然你可以发布一个关于它的新问题,但我想这个问题与你的某些列是值类型有关,因此你不能将它们与null 进行比较,它们应该是Nullable&lt;T&gt;DateTime?。有关更多详细信息,请参阅此问题。 stackoverflow.com/questions/6864311/…
          猜你喜欢
          • 2013-12-22
          • 2015-01-25
          • 2021-09-12
          • 1970-01-01
          • 1970-01-01
          • 2011-08-22
          • 1970-01-01
          • 1970-01-01
          • 2023-04-10
          相关资源
          最近更新 更多