<small id='4FMX4'></small><noframes id='4FMX4'>

    • <bdo id='4FMX4'></bdo><ul id='4FMX4'></ul>

    1. <tfoot id='4FMX4'></tfoot>
        <legend id='4FMX4'><style id='4FMX4'><dir id='4FMX4'><q id='4FMX4'></q></dir></style></legend>
        <i id='4FMX4'><tr id='4FMX4'><dt id='4FMX4'><q id='4FMX4'><span id='4FMX4'><b id='4FMX4'><form id='4FMX4'><ins id='4FMX4'></ins><ul id='4FMX4'></ul><sub id='4FMX4'></sub></form><legend id='4FMX4'></legend><bdo id='4FMX4'><pre id='4FMX4'><center id='4FMX4'></center></pre></bdo></b><th id='4FMX4'></th></span></q></dt></tr></i><div id='4FMX4'><tfoot id='4FMX4'></tfoot><dl id='4FMX4'><fieldset id='4FMX4'></fieldset></dl></div>
      1. 总和可为空的 Linq 查询

        时间:2023-06-05
        • <i id='eK00e'><tr id='eK00e'><dt id='eK00e'><q id='eK00e'><span id='eK00e'><b id='eK00e'><form id='eK00e'><ins id='eK00e'></ins><ul id='eK00e'></ul><sub id='eK00e'></sub></form><legend id='eK00e'></legend><bdo id='eK00e'><pre id='eK00e'><center id='eK00e'></center></pre></bdo></b><th id='eK00e'></th></span></q></dt></tr></i><div id='eK00e'><tfoot id='eK00e'></tfoot><dl id='eK00e'><fieldset id='eK00e'></fieldset></dl></div>

          <small id='eK00e'></small><noframes id='eK00e'>

            <tbody id='eK00e'></tbody>
          <legend id='eK00e'><style id='eK00e'><dir id='eK00e'><q id='eK00e'></q></dir></style></legend>

              <bdo id='eK00e'></bdo><ul id='eK00e'></ul>

            • <tfoot id='eK00e'></tfoot>

                  本文介绍了总和可为空的 Linq 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..
                  from i in Db.Items
                  select new VotedItem
                  {
                      ItemId = i.ItemId,
                      Points = (from v in Db.Votes
                                where b.ItemId == v.ItemId
                                select v.Points).Sum()
                  }
                  

                  我收到了这个查询,但是如果没有发现异常投票,它就会失败:

                  I got this query, however it fails if no votes are found with exception:

                  The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.
                  

                  我假设它是因为 sum 返回一个 int 而不是一个可为空的 int,所以给 sum 一个 int?由于输入只给出相同的错误,可能导致 sum 仅适用于整数.

                  I assume its because sum returns an int and not a nullable int, giving sum a int? as input only give the same error, probably cause sum only workes on ints.

                  有什么好的解决方法吗?

                  Any good workaround for this?

                  推荐答案

                  from i in Db.Items
                  select new VotedItem
                  {
                      ItemId = i.ItemId,
                      Points = (from v in Db.Votes
                                where b.ItemId == v.ItemId
                                select v.Points ?? 0).Sum() 
                  }
                  

                  编辑 - 好吧,这个怎么样...(再次拍摄,因为我不知道你的型号...):

                  EDIT - ok what about this... (Shooting again since I don't know your model...):

                  from i in Db.Items
                  select new VotedItem
                  {
                      ItemId = i.ItemId,
                      Points = (from v in Db.Votes
                                where b.ItemId == v.ItemId)
                                .Sum(v => v.Points) 
                  }
                  

                  这篇关于总和可为空的 Linq 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C#对象列表,我如何获得一个属性的总和 下一篇:如何计算 LINQ 中 DataTable 的列的总和(到数据集)

                  相关文章

                  最新文章

                  <legend id='dyzGH'><style id='dyzGH'><dir id='dyzGH'><q id='dyzGH'></q></dir></style></legend>

                    <small id='dyzGH'></small><noframes id='dyzGH'>

                    • <bdo id='dyzGH'></bdo><ul id='dyzGH'></ul>
                    <i id='dyzGH'><tr id='dyzGH'><dt id='dyzGH'><q id='dyzGH'><span id='dyzGH'><b id='dyzGH'><form id='dyzGH'><ins id='dyzGH'></ins><ul id='dyzGH'></ul><sub id='dyzGH'></sub></form><legend id='dyzGH'></legend><bdo id='dyzGH'><pre id='dyzGH'><center id='dyzGH'></center></pre></bdo></b><th id='dyzGH'></th></span></q></dt></tr></i><div id='dyzGH'><tfoot id='dyzGH'></tfoot><dl id='dyzGH'><fieldset id='dyzGH'></fieldset></dl></div>

                      <tfoot id='dyzGH'></tfoot>