How to Get Values from Igrouping

Get Value property in IGrouping

The group implements IEnumerable<T> - In the general case, just call foreach over the group. In this case, since you need a List<T>:

list.Add(new DespatchGroup(group.Key, group.ToList());

How to get values from IGrouping

Since IGrouping<TKey, TElement> implements IEnumerable<TElement>, you can use SelectMany to put all the IEnumerables back into one IEnumerable all together:

List<smth> list = new List<smth>();
IEnumerable<IGrouping<int, smth>> groups = list.GroupBy(x => x.id);
IEnumerable<smth> smths = groups.SelectMany(group => group);
List<smth> newList = smths.ToList();

Here's an example that builds/runs: https://dotnetfiddle.net/DyuaaP

Video commentary of this solution: https://youtu.be/6BsU1n1KTdo

How to get values out of IGrouping?

Tiers.Select(group => group.Select(element => ...));

How to get value from IGrouping where key and value in object match

First: you're forgetting to add your new ShortCodes to the codesList, so the current code isn't going to give you anything in your lookup.

Second: use ILookup<> rather than Lookup<>. There's no good reason for you to specify the implementation type. If you are only expecting one possible item per combo, use an IDictionary<> instead.

Third: don't use .Where() on a lookup or dictionary. Use [key] syntax instead.

Fourth: key the lookup or dictionary on the values you want to look things up with.

public IDictionary<(string Type, string Code), ShortCode> ShortCodesByTypeAndCode { get; set; }
currentCache.ShortCodesByTypeAndCode = codesList.ToDictionary(code => (code.CodeType, code.Code));
return currentCache.ShortCodesByTypeAndCode[(type, code)].Name;

How can I return the Value from a .NET IGrouping via a Key?

how about a nested loop?

IList<IGrouping<string, PurchaseHistory>> results = someList.GroupBy(x => x.UserName);

foreach (IGrouping<string, PurchaseHistory> group in results)
{
foreach (PurchaseHistory item in group)
{
CheckforStuff(item);
}
}

or one loop with linq statement

IList<IGrouping<string, PurchaseHistory>> results = someList.GroupBy(x => x.UserName);
foreach (IGrouping<string, PurchaseHistory> group in results)
{
bool result = group.Any(item => item.PurchasedOn > someDate);
}

Get first item of IGrouping

try this:

var finalResult = result.Select(gpr=>grp.First());

or if you want the earliest/Latest/etc you could order by first:

var finalResult = result.Select(gpr=>grp.OrderBy(x=>x.SnapShotTime).First());

Getting the Object From IGrouping in c#

After you make the grouping, make a projection to a new anonymous object with a property that will have the value of your key, and another could the the list of grouped values for that key.

var quer2 =
from ff in ddd
from ss in ff.availableHotels.OrderBy(x =>x.totalSalePrice)
group ss by ss.hotelCode
select new
{
GroupKey = ss.Key,
GroupValuesList = ss.ToList()
};

Console.WriteLine(quer2.First().GroupKey);

C# Select Distinct Values from IGrouping

Will there potentially be more than one Bar with the same name? If so, it's tricky. If not, you could just change it to:

var grouped = from f in fooList
orderby f.SomeBar.Name ascending
group f by f.SomeBar into Group
select Group;

var bars = grouped.Select(group => group.Key);

Alternatively, if you only want one Bar per name, you could stick with your original query, but change the last bit:

var someGroup = from f in fooList
orderby f.SomeBar.Name ascending
group f by f.SomeBar.Name into Group
select Group;

var bars = someGroup.Select(group => group.First().SomeBar);

This will take the first Foo in each group, and find its Bar.

How to Make a default value for IGroupingTKey, TSource with count = 0?

How about using C#6 null-propagation like this?

groups.FirstOrDefault(g => g.Key == n)?.Count() ?? 0

if FirstOrDefault returns null, ?.Count() will not be evaluated anymore and not throw an exception.



Related Topics



Leave a reply



Submit