Count every data that grouped in list
up vote
-1
down vote
favorite
I'm trying to count total data that grouped in my list.
So here is my code:
List<InventoryViewModel> temp =
InventoriesByLocation.GroupBy(s => s.ProductId).Select(group => group.First()).ToList();
I want to count how many productId
that already groupby().
c# list linq
add a comment |
up vote
-1
down vote
favorite
I'm trying to count total data that grouped in my list.
So here is my code:
List<InventoryViewModel> temp =
InventoriesByLocation.GroupBy(s => s.ProductId).Select(group => group.First()).ToList();
I want to count how many productId
that already groupby().
c# list linq
2
MoreLINQ's CountBy may be worth considering - markheath.net/post/exploring-morelinq-17-countby .
– mjwills
Nov 11 at 20:50
So basically you want to count how many unique productId's there is in the list?InventoriesByLocation.Select(x => ProductId).Distinct().Count()
– Magnus
Nov 12 at 9:09
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm trying to count total data that grouped in my list.
So here is my code:
List<InventoryViewModel> temp =
InventoriesByLocation.GroupBy(s => s.ProductId).Select(group => group.First()).ToList();
I want to count how many productId
that already groupby().
c# list linq
I'm trying to count total data that grouped in my list.
So here is my code:
List<InventoryViewModel> temp =
InventoriesByLocation.GroupBy(s => s.ProductId).Select(group => group.First()).ToList();
I want to count how many productId
that already groupby().
c# list linq
c# list linq
edited Nov 11 at 20:52
Uwe Keim
27.3k30128210
27.3k30128210
asked Nov 11 at 20:17
Yeremia Danang
457
457
2
MoreLINQ's CountBy may be worth considering - markheath.net/post/exploring-morelinq-17-countby .
– mjwills
Nov 11 at 20:50
So basically you want to count how many unique productId's there is in the list?InventoriesByLocation.Select(x => ProductId).Distinct().Count()
– Magnus
Nov 12 at 9:09
add a comment |
2
MoreLINQ's CountBy may be worth considering - markheath.net/post/exploring-morelinq-17-countby .
– mjwills
Nov 11 at 20:50
So basically you want to count how many unique productId's there is in the list?InventoriesByLocation.Select(x => ProductId).Distinct().Count()
– Magnus
Nov 12 at 9:09
2
2
MoreLINQ's CountBy may be worth considering - markheath.net/post/exploring-morelinq-17-countby .
– mjwills
Nov 11 at 20:50
MoreLINQ's CountBy may be worth considering - markheath.net/post/exploring-morelinq-17-countby .
– mjwills
Nov 11 at 20:50
So basically you want to count how many unique productId's there is in the list?
InventoriesByLocation.Select(x => ProductId).Distinct().Count()
– Magnus
Nov 12 at 9:09
So basically you want to count how many unique productId's there is in the list?
InventoriesByLocation.Select(x => ProductId).Distinct().Count()
– Magnus
Nov 12 at 9:09
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
So you have a sequence of InventoryViewModels
, where every InventoryViewModel
has a ProductId
.
You want to group all InventoryViewModels
into groups, where every groups contains all InventoryViewModels
with the same ProductId
. After that you want to count the number of InventoryViewModels
in each group, and possibly also some other items. Your example code takes the first element in the group.
var groupsWithSameProductId = inventoryViewModels
.GroupBy(inventoryViewModel => inventoryViewModel.ProductId)
Now every group has a Key
, which contains the ProductId
that is the same for all elements in your group. The group itself is a sequence of all InventoryViewModels
that have the ProductId
equal to the Key
.
To get the number of InventoryViewModels
that have this ProductId
, just Count()
them. If you want other items, just Select
them:
// continue the LINQ statement
.Select(group => new
ProductId = group.Key,
NrOfInventroyViewModelsWithThisProductId = group.Count(),
// only if you want other items:
FirstInventoryViewModel = group.First(),
Names = group.Select(groupItem => groupItem.Name).ToList(),
);
Note, that every groupItem
is an InventoryViewModel
with ProductId
equal to group.Key
There is an overload of Enumerable.GroupBy that combines the grouping and selecting in one statement:
var result = inventoryViewModels
.GroupBy(inventoryViewModel => inventoryViewModel.ProductId, // make groups with same productId
(commonProductId, inventoryViewModelsWithThisCommonProductId) => new
ProductId = commonProductId,
Count = inventoryViewModelsWithThisCommonProductId.Count(),
Names = inventoryViewModelsWithThisCommonProductId
.Select(inventoryViewModelWithThisCommonProductId.Name)
.ToList(),
);
thanks for your explanation and i get it. but i gotAnonymous Type: a is newGuid ProductId,int NrOfInventroyViewModelsWithThisProductId
and can i get the same type of list. Or the selected item cant turn into same type of list. In this case:List<inventoryfiewmodel> mInventories= var result
/selected inventorylist/??
– Yeremia Danang
Nov 12 at 12:35
I don't know your classInventoryViewModel
. If it has similar properties, you can usenew InventoryViewModel()
, if it is quite different, then I wonder why you would like to do that. The idea behind object oriented programming is that a dog is different than a cat, so don't try to put a can in a dog object
– Harald Coppoolse
Nov 13 at 10:00
List<InventoryViewModel> InventoriesByLocation = temp.Join(StorageByLocation, i => i.StorageId, s => s.Id, (i, s) => i).Where(i=> i.ExpirationDate<DateTime.Now).GroupBy(s => s.ProductId).Select(g=>new g.Key,Count=g.Count()).ToList();
this is my code. Variable temp isList<inventoryViewModel>
too. I need to put the grouped list toList<InventoryViewModel>
or put them into variabel that I can access publicly. I can't send var (local variable) to my function.
– Yeremia Danang
Nov 14 at 11:34
ohh haha, thanks
– Yeremia Danang
Nov 14 at 11:51
add a comment |
up vote
2
down vote
Something like this:
var count = InventoriesByLocation.GroupBy(s => s.ProductId).Count();
Basically, GroupBy
returns a type roughly equivalent to IDictionary<int, InventoryViewModel>
in your case. So, length of an individual dictionary entry will be the count of a unique ProductId
.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
So you have a sequence of InventoryViewModels
, where every InventoryViewModel
has a ProductId
.
You want to group all InventoryViewModels
into groups, where every groups contains all InventoryViewModels
with the same ProductId
. After that you want to count the number of InventoryViewModels
in each group, and possibly also some other items. Your example code takes the first element in the group.
var groupsWithSameProductId = inventoryViewModels
.GroupBy(inventoryViewModel => inventoryViewModel.ProductId)
Now every group has a Key
, which contains the ProductId
that is the same for all elements in your group. The group itself is a sequence of all InventoryViewModels
that have the ProductId
equal to the Key
.
To get the number of InventoryViewModels
that have this ProductId
, just Count()
them. If you want other items, just Select
them:
// continue the LINQ statement
.Select(group => new
ProductId = group.Key,
NrOfInventroyViewModelsWithThisProductId = group.Count(),
// only if you want other items:
FirstInventoryViewModel = group.First(),
Names = group.Select(groupItem => groupItem.Name).ToList(),
);
Note, that every groupItem
is an InventoryViewModel
with ProductId
equal to group.Key
There is an overload of Enumerable.GroupBy that combines the grouping and selecting in one statement:
var result = inventoryViewModels
.GroupBy(inventoryViewModel => inventoryViewModel.ProductId, // make groups with same productId
(commonProductId, inventoryViewModelsWithThisCommonProductId) => new
ProductId = commonProductId,
Count = inventoryViewModelsWithThisCommonProductId.Count(),
Names = inventoryViewModelsWithThisCommonProductId
.Select(inventoryViewModelWithThisCommonProductId.Name)
.ToList(),
);
thanks for your explanation and i get it. but i gotAnonymous Type: a is newGuid ProductId,int NrOfInventroyViewModelsWithThisProductId
and can i get the same type of list. Or the selected item cant turn into same type of list. In this case:List<inventoryfiewmodel> mInventories= var result
/selected inventorylist/??
– Yeremia Danang
Nov 12 at 12:35
I don't know your classInventoryViewModel
. If it has similar properties, you can usenew InventoryViewModel()
, if it is quite different, then I wonder why you would like to do that. The idea behind object oriented programming is that a dog is different than a cat, so don't try to put a can in a dog object
– Harald Coppoolse
Nov 13 at 10:00
List<InventoryViewModel> InventoriesByLocation = temp.Join(StorageByLocation, i => i.StorageId, s => s.Id, (i, s) => i).Where(i=> i.ExpirationDate<DateTime.Now).GroupBy(s => s.ProductId).Select(g=>new g.Key,Count=g.Count()).ToList();
this is my code. Variable temp isList<inventoryViewModel>
too. I need to put the grouped list toList<InventoryViewModel>
or put them into variabel that I can access publicly. I can't send var (local variable) to my function.
– Yeremia Danang
Nov 14 at 11:34
ohh haha, thanks
– Yeremia Danang
Nov 14 at 11:51
add a comment |
up vote
1
down vote
accepted
So you have a sequence of InventoryViewModels
, where every InventoryViewModel
has a ProductId
.
You want to group all InventoryViewModels
into groups, where every groups contains all InventoryViewModels
with the same ProductId
. After that you want to count the number of InventoryViewModels
in each group, and possibly also some other items. Your example code takes the first element in the group.
var groupsWithSameProductId = inventoryViewModels
.GroupBy(inventoryViewModel => inventoryViewModel.ProductId)
Now every group has a Key
, which contains the ProductId
that is the same for all elements in your group. The group itself is a sequence of all InventoryViewModels
that have the ProductId
equal to the Key
.
To get the number of InventoryViewModels
that have this ProductId
, just Count()
them. If you want other items, just Select
them:
// continue the LINQ statement
.Select(group => new
ProductId = group.Key,
NrOfInventroyViewModelsWithThisProductId = group.Count(),
// only if you want other items:
FirstInventoryViewModel = group.First(),
Names = group.Select(groupItem => groupItem.Name).ToList(),
);
Note, that every groupItem
is an InventoryViewModel
with ProductId
equal to group.Key
There is an overload of Enumerable.GroupBy that combines the grouping and selecting in one statement:
var result = inventoryViewModels
.GroupBy(inventoryViewModel => inventoryViewModel.ProductId, // make groups with same productId
(commonProductId, inventoryViewModelsWithThisCommonProductId) => new
ProductId = commonProductId,
Count = inventoryViewModelsWithThisCommonProductId.Count(),
Names = inventoryViewModelsWithThisCommonProductId
.Select(inventoryViewModelWithThisCommonProductId.Name)
.ToList(),
);
thanks for your explanation and i get it. but i gotAnonymous Type: a is newGuid ProductId,int NrOfInventroyViewModelsWithThisProductId
and can i get the same type of list. Or the selected item cant turn into same type of list. In this case:List<inventoryfiewmodel> mInventories= var result
/selected inventorylist/??
– Yeremia Danang
Nov 12 at 12:35
I don't know your classInventoryViewModel
. If it has similar properties, you can usenew InventoryViewModel()
, if it is quite different, then I wonder why you would like to do that. The idea behind object oriented programming is that a dog is different than a cat, so don't try to put a can in a dog object
– Harald Coppoolse
Nov 13 at 10:00
List<InventoryViewModel> InventoriesByLocation = temp.Join(StorageByLocation, i => i.StorageId, s => s.Id, (i, s) => i).Where(i=> i.ExpirationDate<DateTime.Now).GroupBy(s => s.ProductId).Select(g=>new g.Key,Count=g.Count()).ToList();
this is my code. Variable temp isList<inventoryViewModel>
too. I need to put the grouped list toList<InventoryViewModel>
or put them into variabel that I can access publicly. I can't send var (local variable) to my function.
– Yeremia Danang
Nov 14 at 11:34
ohh haha, thanks
– Yeremia Danang
Nov 14 at 11:51
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
So you have a sequence of InventoryViewModels
, where every InventoryViewModel
has a ProductId
.
You want to group all InventoryViewModels
into groups, where every groups contains all InventoryViewModels
with the same ProductId
. After that you want to count the number of InventoryViewModels
in each group, and possibly also some other items. Your example code takes the first element in the group.
var groupsWithSameProductId = inventoryViewModels
.GroupBy(inventoryViewModel => inventoryViewModel.ProductId)
Now every group has a Key
, which contains the ProductId
that is the same for all elements in your group. The group itself is a sequence of all InventoryViewModels
that have the ProductId
equal to the Key
.
To get the number of InventoryViewModels
that have this ProductId
, just Count()
them. If you want other items, just Select
them:
// continue the LINQ statement
.Select(group => new
ProductId = group.Key,
NrOfInventroyViewModelsWithThisProductId = group.Count(),
// only if you want other items:
FirstInventoryViewModel = group.First(),
Names = group.Select(groupItem => groupItem.Name).ToList(),
);
Note, that every groupItem
is an InventoryViewModel
with ProductId
equal to group.Key
There is an overload of Enumerable.GroupBy that combines the grouping and selecting in one statement:
var result = inventoryViewModels
.GroupBy(inventoryViewModel => inventoryViewModel.ProductId, // make groups with same productId
(commonProductId, inventoryViewModelsWithThisCommonProductId) => new
ProductId = commonProductId,
Count = inventoryViewModelsWithThisCommonProductId.Count(),
Names = inventoryViewModelsWithThisCommonProductId
.Select(inventoryViewModelWithThisCommonProductId.Name)
.ToList(),
);
So you have a sequence of InventoryViewModels
, where every InventoryViewModel
has a ProductId
.
You want to group all InventoryViewModels
into groups, where every groups contains all InventoryViewModels
with the same ProductId
. After that you want to count the number of InventoryViewModels
in each group, and possibly also some other items. Your example code takes the first element in the group.
var groupsWithSameProductId = inventoryViewModels
.GroupBy(inventoryViewModel => inventoryViewModel.ProductId)
Now every group has a Key
, which contains the ProductId
that is the same for all elements in your group. The group itself is a sequence of all InventoryViewModels
that have the ProductId
equal to the Key
.
To get the number of InventoryViewModels
that have this ProductId
, just Count()
them. If you want other items, just Select
them:
// continue the LINQ statement
.Select(group => new
ProductId = group.Key,
NrOfInventroyViewModelsWithThisProductId = group.Count(),
// only if you want other items:
FirstInventoryViewModel = group.First(),
Names = group.Select(groupItem => groupItem.Name).ToList(),
);
Note, that every groupItem
is an InventoryViewModel
with ProductId
equal to group.Key
There is an overload of Enumerable.GroupBy that combines the grouping and selecting in one statement:
var result = inventoryViewModels
.GroupBy(inventoryViewModel => inventoryViewModel.ProductId, // make groups with same productId
(commonProductId, inventoryViewModelsWithThisCommonProductId) => new
ProductId = commonProductId,
Count = inventoryViewModelsWithThisCommonProductId.Count(),
Names = inventoryViewModelsWithThisCommonProductId
.Select(inventoryViewModelWithThisCommonProductId.Name)
.ToList(),
);
answered Nov 12 at 8:17
Harald Coppoolse
11.2k12959
11.2k12959
thanks for your explanation and i get it. but i gotAnonymous Type: a is newGuid ProductId,int NrOfInventroyViewModelsWithThisProductId
and can i get the same type of list. Or the selected item cant turn into same type of list. In this case:List<inventoryfiewmodel> mInventories= var result
/selected inventorylist/??
– Yeremia Danang
Nov 12 at 12:35
I don't know your classInventoryViewModel
. If it has similar properties, you can usenew InventoryViewModel()
, if it is quite different, then I wonder why you would like to do that. The idea behind object oriented programming is that a dog is different than a cat, so don't try to put a can in a dog object
– Harald Coppoolse
Nov 13 at 10:00
List<InventoryViewModel> InventoriesByLocation = temp.Join(StorageByLocation, i => i.StorageId, s => s.Id, (i, s) => i).Where(i=> i.ExpirationDate<DateTime.Now).GroupBy(s => s.ProductId).Select(g=>new g.Key,Count=g.Count()).ToList();
this is my code. Variable temp isList<inventoryViewModel>
too. I need to put the grouped list toList<InventoryViewModel>
or put them into variabel that I can access publicly. I can't send var (local variable) to my function.
– Yeremia Danang
Nov 14 at 11:34
ohh haha, thanks
– Yeremia Danang
Nov 14 at 11:51
add a comment |
thanks for your explanation and i get it. but i gotAnonymous Type: a is newGuid ProductId,int NrOfInventroyViewModelsWithThisProductId
and can i get the same type of list. Or the selected item cant turn into same type of list. In this case:List<inventoryfiewmodel> mInventories= var result
/selected inventorylist/??
– Yeremia Danang
Nov 12 at 12:35
I don't know your classInventoryViewModel
. If it has similar properties, you can usenew InventoryViewModel()
, if it is quite different, then I wonder why you would like to do that. The idea behind object oriented programming is that a dog is different than a cat, so don't try to put a can in a dog object
– Harald Coppoolse
Nov 13 at 10:00
List<InventoryViewModel> InventoriesByLocation = temp.Join(StorageByLocation, i => i.StorageId, s => s.Id, (i, s) => i).Where(i=> i.ExpirationDate<DateTime.Now).GroupBy(s => s.ProductId).Select(g=>new g.Key,Count=g.Count()).ToList();
this is my code. Variable temp isList<inventoryViewModel>
too. I need to put the grouped list toList<InventoryViewModel>
or put them into variabel that I can access publicly. I can't send var (local variable) to my function.
– Yeremia Danang
Nov 14 at 11:34
ohh haha, thanks
– Yeremia Danang
Nov 14 at 11:51
thanks for your explanation and i get it. but i got
Anonymous Type: a is newGuid ProductId,int NrOfInventroyViewModelsWithThisProductId
and can i get the same type of list. Or the selected item cant turn into same type of list. In this case: List<inventoryfiewmodel> mInventories= var result
/selected inventorylist/??– Yeremia Danang
Nov 12 at 12:35
thanks for your explanation and i get it. but i got
Anonymous Type: a is newGuid ProductId,int NrOfInventroyViewModelsWithThisProductId
and can i get the same type of list. Or the selected item cant turn into same type of list. In this case: List<inventoryfiewmodel> mInventories= var result
/selected inventorylist/??– Yeremia Danang
Nov 12 at 12:35
I don't know your class
InventoryViewModel
. If it has similar properties, you can use new InventoryViewModel()
, if it is quite different, then I wonder why you would like to do that. The idea behind object oriented programming is that a dog is different than a cat, so don't try to put a can in a dog object– Harald Coppoolse
Nov 13 at 10:00
I don't know your class
InventoryViewModel
. If it has similar properties, you can use new InventoryViewModel()
, if it is quite different, then I wonder why you would like to do that. The idea behind object oriented programming is that a dog is different than a cat, so don't try to put a can in a dog object– Harald Coppoolse
Nov 13 at 10:00
List<InventoryViewModel> InventoriesByLocation = temp.Join(StorageByLocation, i => i.StorageId, s => s.Id, (i, s) => i).Where(i=> i.ExpirationDate<DateTime.Now).GroupBy(s => s.ProductId).Select(g=>new g.Key,Count=g.Count()).ToList();
this is my code. Variable temp is List<inventoryViewModel>
too. I need to put the grouped list to List<InventoryViewModel>
or put them into variabel that I can access publicly. I can't send var (local variable) to my function.– Yeremia Danang
Nov 14 at 11:34
List<InventoryViewModel> InventoriesByLocation = temp.Join(StorageByLocation, i => i.StorageId, s => s.Id, (i, s) => i).Where(i=> i.ExpirationDate<DateTime.Now).GroupBy(s => s.ProductId).Select(g=>new g.Key,Count=g.Count()).ToList();
this is my code. Variable temp is List<inventoryViewModel>
too. I need to put the grouped list to List<InventoryViewModel>
or put them into variabel that I can access publicly. I can't send var (local variable) to my function.– Yeremia Danang
Nov 14 at 11:34
ohh haha, thanks
– Yeremia Danang
Nov 14 at 11:51
ohh haha, thanks
– Yeremia Danang
Nov 14 at 11:51
add a comment |
up vote
2
down vote
Something like this:
var count = InventoriesByLocation.GroupBy(s => s.ProductId).Count();
Basically, GroupBy
returns a type roughly equivalent to IDictionary<int, InventoryViewModel>
in your case. So, length of an individual dictionary entry will be the count of a unique ProductId
.
add a comment |
up vote
2
down vote
Something like this:
var count = InventoriesByLocation.GroupBy(s => s.ProductId).Count();
Basically, GroupBy
returns a type roughly equivalent to IDictionary<int, InventoryViewModel>
in your case. So, length of an individual dictionary entry will be the count of a unique ProductId
.
add a comment |
up vote
2
down vote
up vote
2
down vote
Something like this:
var count = InventoriesByLocation.GroupBy(s => s.ProductId).Count();
Basically, GroupBy
returns a type roughly equivalent to IDictionary<int, InventoryViewModel>
in your case. So, length of an individual dictionary entry will be the count of a unique ProductId
.
Something like this:
var count = InventoriesByLocation.GroupBy(s => s.ProductId).Count();
Basically, GroupBy
returns a type roughly equivalent to IDictionary<int, InventoryViewModel>
in your case. So, length of an individual dictionary entry will be the count of a unique ProductId
.
edited Nov 11 at 20:52
mjwills
14.6k42439
14.6k42439
answered Nov 11 at 20:20
Sergey Shulik
678824
678824
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252830%2fcount-every-data-that-grouped-in-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
MoreLINQ's CountBy may be worth considering - markheath.net/post/exploring-morelinq-17-countby .
– mjwills
Nov 11 at 20:50
So basically you want to count how many unique productId's there is in the list?
InventoriesByLocation.Select(x => ProductId).Distinct().Count()
– Magnus
Nov 12 at 9:09