如何在 Linq 语句中进行转换并分配正确的值

发布于 2024-08-21 10:35:44 字数 720 浏览 1 评论 0原文

我一直在尝试以基于表达式的方式编写以下代码,但不确定如何在比较后对对象进行赋值。任何帮助将不胜感激。

var pcs = from a in collection
      group a by a.TotType
      into g
      select new
      {
          TType = g.Key,
          SColl = g.Select(r => r)
     };

for (int i = 0; i < processResult.PAndLReport.BreakdownTotTypeCollection.Count; i++)
{
  foreach (var ttypes in pcs)
  {
    if (processResult.PAndLReport.BreakdownTotTypeCollection[i].TotType == ttypes.TType)
    {
      BreakdownCollection coll = new BreakdownCollection();
      foreach (var item1 in ttypes.SColl)
       {
         coll.Add(item1);
       }

      processResult.PAndLReport.BreakdownTotTypeCollection[i].BreakdownCollection = coll;
     }
  }
}

I been trying to write the following code in expression based way but not sure how can i do that assignment to the object after comparison. Any help would be highly appreciated.

var pcs = from a in collection
      group a by a.TotType
      into g
      select new
      {
          TType = g.Key,
          SColl = g.Select(r => r)
     };

for (int i = 0; i < processResult.PAndLReport.BreakdownTotTypeCollection.Count; i++)
{
  foreach (var ttypes in pcs)
  {
    if (processResult.PAndLReport.BreakdownTotTypeCollection[i].TotType == ttypes.TType)
    {
      BreakdownCollection coll = new BreakdownCollection();
      foreach (var item1 in ttypes.SColl)
       {
         coll.Add(item1);
       }

      processResult.PAndLReport.BreakdownTotTypeCollection[i].BreakdownCollection = coll;
     }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏别ゝ 2024-08-28 10:35:44

所以 - 代码可以工作,但是非常冗长。

这是减少代码的尝试。我做了一些假设(此代码并不执行与其他代码完全相同的分配),但它可能接近您想要的。

var pcs = collection.ToLookup(a => a.TotType);

foreach(var bttcoll in processResult
    .PAndLReport
    .BreakdownTotTypeCollection)
{
  var items = pcs[bttcoll.ToTType];
    //do you have a constructor that takes an IEnumerable<Item> ?
  bttcoll.BreakdownCollection = new BreakdownCollection(items)
}

一般来说,您应该避免更改 linq 查询内的对象状态。 Linq 来自函数式编程思维,其中状态永远不会改变(相反,会创建具有所需状态的新对象)。

So - the code works, but it's very verbose.

Here's a stab at reducing the code. I've made some assumptions (this code doesn't do exactly the same assignments as the other code), but it might be close to what you want.

var pcs = collection.ToLookup(a => a.TotType);

foreach(var bttcoll in processResult
    .PAndLReport
    .BreakdownTotTypeCollection)
{
  var items = pcs[bttcoll.ToTType];
    //do you have a constructor that takes an IEnumerable<Item> ?
  bttcoll.BreakdownCollection = new BreakdownCollection(items)
}

In general, you should avoid changing object state inside of a linq query. Linq is from the functional programming mindset, where state never changes (instead, new objects are created with the needed state).

み青杉依旧 2024-08-28 10:35:44

如果 David B 给出的答案有效,那就太好了,但不要在代码中使用它。第一个(大概)有效,并且更容易让下一个人理解。

需要考虑的一些事情:

  1. 如果您的代码将使用很长时间,那么维护它的时间将比最初编写的时间多得多。当你忘记自己到底在做什么后不久,你就必须改变行为。让自己变得更轻松。
  2. 如果你的代码不会使用很长时间,那么你花了更多的时间来调整风格和流程,然后就会恢复。
  3. 与声明性样式相比,使用表达式样式 Linq 没有速度优势。编译器将为您转换代码,并且在大多数情况下将与您自己编写的代码一样高效。

If the answer David B gives works, that is great, but DO NOT use it in your code. The first one (presumably) works and is much easier for the next guy to comprehend.

Some things to think about:

  1. If your code will be used for a long time then much more time will be spent maintaining it then in the original writing of it. You will have to modify the behavior shortly after you have forgotten exactly what it was you were doing. Make it easier on yourself.
  2. If your code will not be used for long, then you have spent more time in tweaking the style and flow then will be recovered.
  3. There is no speed benefit in using expression style Linq over declarative style. The compiler will convert the code for you and in most cases will be just as efficient as what you could write yourself.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文