Created
March 28, 2018 06:29
-
-
Save amantix/395a871626b37fa2e487bb2fe2f7d93f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void LinqObj76() | |
{ | |
var goods = ReadItems<Item>("goods.txt"); | |
var prices = ReadItems<Cost>("prices.txt"); | |
var result = goods | |
.GroupJoin(prices, g => g.Article, p => p.ItemArticle, | |
(g, p) => p.DefaultIfEmpty().Select(y => new {g.Country, y?.ShopName, y?.Price})) | |
.SelectMany(x => x) | |
.GroupBy(x => x.Country) | |
.Select(x => new | |
{ | |
Country = x.Key, | |
Count = x.Where(y => !string.IsNullOrEmpty(y.ShopName)) | |
.Select(y => y.ShopName) | |
.Distinct() | |
.Count(), | |
MinPrice = x.Min(y => y.Price) ?? 0 | |
}); | |
var resultQuery = | |
from g in goods | |
join p in prices | |
on g.Article equals p.ItemArticle | |
into pMatch | |
from p in pMatch.DefaultIfEmpty() | |
select new {g.Country, p?.ShopName, p?.Price} | |
into seq | |
group seq by seq.Country | |
into countryGroup | |
select new | |
{ | |
Country = countryGroup.Key, | |
Count = countryGroup | |
.Where(y => !string.IsNullOrEmpty(y.ShopName)) | |
.Select(y => y.ShopName) | |
.Distinct() | |
.Count(), | |
MinPrice = countryGroup.Min(y => y.Price) ?? 0 | |
}; | |
Console.WriteLine(string.Join("\n", result)); | |
Console.WriteLine(); | |
Console.WriteLine(string.Join("\n", resultQuery)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment