# LINQ用于不同地址的总和[英] Linq for sum of distinct addresses

### 问题描述

```Product weight  address1    address2    address3            city            state               postcode    country
A123    6       House       1st Street  some place          a city          a state             AB1 2CD     GB
A456    3       House       1st Street  some place          a city          a state             AB1 2CD     GB
A789    4       House       1st Street  some place          a city          a state             AB1 2CD     GB
A123    6       House2      2st Street  another place       another city    another state       EF2 3GH     GB
A456    3       House2      2st Street  another place       another city    another state       EF2 3GH     GB
A789    4       House2      2st Street  another place       another city    another state       EF2 3GH     GB
```

## 推荐答案

```var query =
from p in table.AsEnumerable()
group p by new {
City = p.Field<string>("city"),
State = p.Field<string>("state"),
Postcode = p.Field<string>("postcode"),
Country = p.Field<string>("country")
} into g
select new {
TotalWeight = g.Sum(x => x.Field<int>("weight"))
};
```

## 其他推荐答案

GroupBy()将将所有产品分为每个不同地址的子收集. 然后，Select()总计每个子收集的重量以提供总重量.

```var totals = products
.GroupBy(p => new
{
city = p.Field<string>("city"),
state = p.Field<string>("state"),
postcode = p.Field<string>("postcode"),
country = p.Field<string>("country")
})
.Select(g => new
{
Total = g.Sum(p => p.Field<int>("weight"),
Country = g.Key.country
});
```

```foreach (var address in totals)
{
}
```

### 问题描述

I have a DataTable of Products. Each product has a weight and a return address. The return address is comprised of 7 fields.

I need to cycle through the distinct addresses and sum up the total weight of product.

Example table would look like this...

```Product weight  address1    address2    address3            city            state               postcode    country
A123    6       House       1st Street  some place          a city          a state             AB1 2CD     GB
A456    3       House       1st Street  some place          a city          a state             AB1 2CD     GB
A789    4       House       1st Street  some place          a city          a state             AB1 2CD     GB
A123    6       House2      2st Street  another place       another city    another state       EF2 3GH     GB
A456    3       House2      2st Street  another place       another city    another state       EF2 3GH     GB
A789    4       House2      2st Street  another place       another city    another state       EF2 3GH     GB
```

I would have 2 addresses returning a weight of 13.

I only need to group by the address fields (not product) and sum the weight by the address. I also need to return the country as well as the summed weight.

Is this possible using linq? Or would I be better using a SqlDataAdaptor on the DataTable? I know how I could do with with the SqlDataAdaptor but I don't know how to do with Linq and I'm guessing linq would be better for overhead?

## 推荐答案

Group table rows by all address fields, and the calculate sum for each group:

```var query =
from p in table.AsEnumerable()
group p by new {
City = p.Field<string>("city"),
State = p.Field<string>("state"),
Postcode = p.Field<string>("postcode"),
Country = p.Field<string>("country")
} into g
select new {
TotalWeight = g.Sum(x => x.Field<int>("weight"))
};
```

That will give you sequence of anonymous objects, which will have all address fields in Address property and sum of weights in TotalWeight property.

## 其他推荐答案

The GroupBy() will group all of the products into sub-collections per distinct address. The Select() is then totalling up the weight of each sub-collection to provide the total weight.

```var totals = products
.GroupBy(p => new
{
city = p.Field<string>("city"),
state = p.Field<string>("state"),
postcode = p.Field<string>("postcode"),
country = p.Field<string>("country")
})
.Select(g => new
{
Total = g.Sum(p => p.Field<int>("weight"),
Country = g.Key.country
});
```

Example use:

```foreach (var address in totals)
{