如果我使用 linq 进行投影而不使用范围变量,正确的语法是什么?

发布于 2024-09-02 03:43:07 字数 652 浏览 3 评论 0原文

我有一个对大量数据进行求和和聚合的查询,如下所示:

var anonType = from x in collection
               let Cars = collection.Where(c=>c.Code == "Cars")
               let Trucks = collection.Where(c=>c.Code == "Trucks")
               select new { 
                           Total = collection.Sum(v=>v.Amount),
                           CarValue = Cars.Sum(v=>v.Amout),
                           TruckValue = Trucks.Sum(v=>v.Amount),
                           CarCount = Cars.Count(),
                           TruckCount = Trucks.Count()
               };

我发现我必须声明范围变量 x 真的很奇怪,特别是如果我不使用它的话。那么,我是否做错了什么,或者我应该遵循不同的格式?

I have a query that sums and aggregates alot of data something like this:

var anonType = from x in collection
               let Cars = collection.Where(c=>c.Code == "Cars")
               let Trucks = collection.Where(c=>c.Code == "Trucks")
               select new { 
                           Total = collection.Sum(v=>v.Amount),
                           CarValue = Cars.Sum(v=>v.Amout),
                           TruckValue = Trucks.Sum(v=>v.Amount),
                           CarCount = Cars.Count(),
                           TruckCount = Trucks.Count()
               };

I find it really weird that I have to declare the range variable x, especially if I'm not using it. So, am I doing something wrong or is there a different format I should be following?

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

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

发布评论

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

评论(3

云之铃。 2024-09-09 03:43:07

我可能是错的,但从您的使用情况来看,我认为您无论如何都不想对集合进行传统的查询表达式语法查询,因为您似乎只是在寻找聚合。按照您编写的方式,您将提取聚合数据的多个副本,因为您正在为集合中的每个项目执行此操作。如果您愿意,您可以像这样拆分查询(放入示例属性)

var values = collection.Where(c => c.Code == "A");
var anonType = new
               {
                   Sum = values.Sum(v => v.Amount),
                   MinimumStartDate = values.Min(v => v.StartDate),
                   Count = values.Count()
               };

I could be wrong, but from your usage, I don't think you want to do a traditional query expression syntax query with your collection anyway, as it appears you are only looking for aggregates. The way you have it written, you would be pulling multiple copies of the aggregated data because you're doing it for each of the items in the collection. If you wished, you could split your query like this (sample properties thrown in)

var values = collection.Where(c => c.Code == "A");
var anonType = new
               {
                   Sum = values.Sum(v => v.Amount),
                   MinimumStartDate = values.Min(v => v.StartDate),
                   Count = values.Count()
               };
清旖 2024-09-09 03:43:07

无论循环结构如何,都可以声明范围变量:

foreach(var x in collection)

or

for(var index = 0; index < collection.Count; index++)

var index = 0;

while(index < collection.Count)
{
    //...

    index++;
}

查询没有什么不同。只是不要使用该变量,它不会造成任何伤害。

You declare a range variable no matter the looping construct:

foreach(var x in collection)

or

for(var index = 0; index < collection.Count; index++)

or

var index = 0;

while(index < collection.Count)
{
    //...

    index++;
}

Queries are no different. Just don't use the variable, it doesn't hurt anything.

韬韬不绝 2024-09-09 03:43:07

那么,我做错了什么吗?

你的查询不好。对于集合中的每个元素,您将枚举集合 5 次(成本 = 5*n^2)。

我应该遵循不同的格式吗?

您可以枚举集合 5 次(成本 = 5n)。

IEnumerable<X> cars = collection.Where(c => c.Code == "Cars");
IEnumerable<X> trucks = collection.Where(c => c.Code == "Trucks");

var myTotals = new 
{
  Total = collection.Sum(v => v.Amount),
  CarValue = cars.Sum(v => v.Amount),
  TruckValue = trucks.Sum(v => v.Amount,
  CarCount = cars.Count(),
  TruckCount = trucks.Count()
};

So, am I doing something wrong?

Your query is not good. For each element in the collection, you are enumerating the collection 5 times (cost = 5*n^2).

Is there a different format I should be following?

You could get away with enumerating the collection 5 times (cost = 5n).

IEnumerable<X> cars = collection.Where(c => c.Code == "Cars");
IEnumerable<X> trucks = collection.Where(c => c.Code == "Trucks");

var myTotals = new 
{
  Total = collection.Sum(v => v.Amount),
  CarValue = cars.Sum(v => v.Amount),
  TruckValue = trucks.Sum(v => v.Amount,
  CarCount = cars.Count(),
  TruckCount = trucks.Count()
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文