Question : I have a list of items which I need to first group and then sub group by the number of items in their product group. Code below. The objective is to create matching groups where the zones and the products available in each match. The zone and product are subject to change, but the number products available should always be grouped.
for example. given the below...
Result should be...
Group 1
Zone = "EAST", Product = "Bananas", ShippingTime = "3_Days" };
Zone = "EAST", Product = "Oranges", ShippingTime = "5_Days" };
Zone = "SOUTH", Product = "Bananas", ShippingTime = "3_Days" };
Zone = "SOUTH", Product = "Oranges", ShippingTime = "10_Days" };
Group 2
Zone = "WEST", Product = "Oranges", ShippingTime = "3_Days" };
Zone = "WEST", Product = "Oranges", ShippingTime = "5_Days" };
Zone = "WEST", Product = "Oranges", ShippingTime = "10_Days" };
Zone = "WEST", Product = "Apples", ShippingTime = "3_Days" };
Zone = "WEST", Product = "Apples", ShippingTime = "5_Days" };
Zone = "WEST", Product = "Apples", ShippingTime = "5_Days" };
Zone = "WEST", Product = "Bananas", ShippingTime = "3_Days" };
Zone = "WEST", Product = "Bananas", ShippingTime = "5_Days" };
Zone = "WEST", Product = "Bananas", ShippingTime = "5_Days" };
The ideal grouping would be to break out the below from Group 2 (caus' they match what's in group 1) and add to
group 1, thus leaving the residual in group 2.
Zone = "WEST", Product = "Bananas", ShippingTime = "3_Days" };
Zone = "WEST", Product = "Oranges", ShippingTime = "10_Days" };
Here's the test I've been working with. Nothing I've been able to do with linq seems to get the job done.
Thanks in Advance for the ideas.
public void should_group_products_and_shippingtimes()
{
{
Bananas = "Limited DR";
var a = new MyClass { Id = 1, Zone = "EAST", Product = "Bananas", ShippingTime = "3_Days" };
var b = new MyClass { Id = 2, Zone = "EAST", Product = "Oranges", ShippingTime = "5_Days" };
var c = new MyClass { Id = 3, Zone = "SOUTH", Product = "Bananas", ShippingTime = "3_Days" };
var d = new MyClass { Id = 4, Zone = "SOUTH", Product = "Oranges", ShippingTime = "10_Days" };
var e = new MyClass { Id = 5, Zone = "WEST", Product = "Oranges", ShippingTime = "3_Days" };
var f = new MyClass { Id = 6, Zone = "WEST", Product = "Oranges", ShippingTime = "5_Days" };
var g = new MyClass { Id = 7, Zone = "WEST", Product = "Oranges", ShippingTime = "10_Days" };
var h = new MyClass { Id = 8, Zone = "WEST", Product = "Apples", ShippingTime = "3_Days" };
var i = new MyClass { Id = 9, Zone = "WEST", Product = "Apples", ShippingTime = "5_Days" };
var j = new MyClass { Id = 10, Zone = "WEST", Product = "Apples", ShippingTime = "5_Days" };
var k = new MyClass { Id = 11, Zone = "WEST", Product = "Bananas", ShippingTime = "3_Days" };
var l = new MyClass { Id = 12, Zone = "WEST", Product = "Bananas", ShippingTime = "5_Days" };
var m = new MyClass { Id = 13, Zone = "WEST", Product = "Bananas", ShippingTime = "5_Days" };
var myList = new List<MyClass>();
myList.AddRange(new[] {a,b,c,d,e,f,g,h,i,j,k,l,m});
var sublist = (from ee in myList
from ff in myList
where ee.Product == ff.Product
&& ee.Id != ff.Id
select ee).Distinct();
var match1 =
myList.AsEnumerable().GroupBy(
record =>
new { PRODUCT = record.Product, SHIPPINGTIME = record.ShippingTime }).Where(z => z.Count() == 1);
var match2 =
myList.AsEnumerable().GroupBy(
record =>
new { PRODUCT = record.Product, SHIPPINGTIME = record.ShippingTime }).Where(z => z.Count() == 2);
var match3 =
myList.AsEnumerable().GroupBy(
record =>
new { PRODUCT = record.Product, SHIPPINGTIME = record.ShippingTime }).Where(z => z.Count() == 3);
var match4 =
myList.AsEnumerable().GroupBy(
record =>
new { PRODUCT = record.Product, SHIPPINGTIME = record.ShippingTime }).Where(z => z.Count() == 4);
var match5 =
myList.AsEnumerable().GroupBy(
record =>
new { PRODUCT = record.Product, SHIPPINGTIME = record.ShippingTime }).Where(z => z.Count() == 5);
// Get the total from each of these group where they match, throw the rest out.
foreach (var entry in sublist)
{
Console.WriteLine(entry);
}
Assert.That(sublist, Is.Not.Null);
}
}
// Supporting Class
public class MyClass
{
public int Id { get; set; }
public string Zone { get; set; }
public string Product { get; set; }
public string ShippingTime { get; set; }
}
I'm not sure I understood you right, but I think you want to do something like that:
myList.Select(record => record.Product)
.Distinct()
.Select(p => new
{
Product = p,
Zones = myList.Where(r => r.Product == p)
.Select(r => r.Zone)
.Distinct()
})
.GroupBy(an => an.Zones.Count())
Related
I have a list of order items with the given structure :
OrderItem { Id = 1, Name = "First Item", Quantity = 2 }
OrderItem { Id = 2, Name = "Second Item", Quantity = 2 }
OrderItem { Id = 3, Name = "Third Item", Quantity = 1 }
I want to flatten it to the following structure :
DBItem{ Id = 1, Name = "First Item" }
DBItem{ Id = 2, Name = "First Item" }
DBItem{ Id = 3, Name = "Second Item" }
DBItem{ Id = 4, Name = "Second Item" }
DBItem{ Id = 5, Name = "Third Item" }
Is there a way using LINQ SelectMany?
You can use Enumerable.Range to generate aditional records:
var dbItems = orderItems
.OrderBy(oi => oi.Id) // ensure correct order
.SelectMany(oi => Enumerable.Range(0, oi.Quantity), (oi, n) => oi) // records duplication
.Select((oi, idx) => new DBItem // Select overload with index
{
Id = idx + 1,
Name = oi.Name,
})
.ToList();
This seems quite straight forward to me:
var items = new[]
{
new OrderItem { Id = 1,Name = "First Item", Quantity = 2 },
new OrderItem { Id = 2,Name = "Second Item", Quantity = 2 },
new OrderItem { Id = 3,Name = "Third Item", Quantity = 1 },
};
var dbItems =
items
.SelectMany(item => Enumerable.Repeat(item.Name, item.Quantity))
.Select((name, index) => new DBItem { Id = index + 1, Name = name });
var items = new List<OrderItem>
{
new OrderItem { Id = 1,Name = "First Item", Quantity = 2 },
new OrderItem { Id = 2,Name = "Second Item", Quantity = 2 },
new OrderItem { Id = 3,Name = "Third Item", Quantity = 1 },
};
var dbitems = new List<DBItem>();
var counter = 1;
items.ForEach(item =>
{
for (int i = 0; i < item.Quantity; i++)
{
dbitems.Add(new DBItem
{
Id = counter++,
Name = item.Name,
});
}
});
I have these lists:
var subjects = new List<SubjectModel>
{
new SubjectModel { subjId = 1, subjName = "Math" },
new SubjectModel { subjId = 2, subjName = "Science" },
new SubjectModel { subjId = 3, subjName = "History" },
new SubjectModel { subjId = 4, subjName = "Language" }
};
var quizzes = new List<QuizModel>
{
new QuizModel { quizId = 1, quizDate = DateTime.Parse("2016-11-25"), quizScore = 10, subjectId = 1 },
new QuizModel { quizId = 2, quizDate = DateTime.Parse("2016-11-25"), quizScore = 15, subjectId = 1 },
new QuizModel { quizId = 3, quizDate = DateTime.Parse("2016-11-25"), quizScore = 8, subjectId = 2 },
new QuizModel { quizId = 4, quizDate = DateTime.Parse("2016-11-26"), quizScore = 13, subjectId = 1 },
new QuizModel { quizId = 5, quizDate = DateTime.Parse("2016-11-26"), quizScore = 20, subjectId = 2 }
};
var exams = new List<ExamModel>
{
new ExamModel { examId = 1, examDate = DateTime.Parse("2016-11-25"), examScore = 90, subjectId = 1 },
new ExamModel { examId = 2, examDate = DateTime.Parse("2016-11-25"), examScore = 88, subjectId = 2 },
new ExamModel { examId = 3, examDate = DateTime.Parse("2016-11-25"), examScore = 92, subjectId = 4 },
new ExamModel { examId = , examDate = DateTime.Parse("2016-11-26"), examScore = 84, subjectId = 1 },
};
var exercises = new List<ExerciseModel>
{
new ExerciseModel { exerciseId = 1, exerciseDate = DateTime.Parse("2016-11-25"), exerciseScore = 17, subjectId = 1 },
new ExerciseModel { exerciseId = 2, exerciseDate = DateTime.Parse("2016-11-25"), exerciseScore = 15, subjectId = 2 },
new ExerciseModel { exerciseId = 3, exerciseDate = DateTime.Parse("2016-11-26"), exerciseScore = 15, subjectId = 1 },
new ExerciseModel { exerciseId = 4, exerciseDate = DateTime.Parse("2016-11-26"), exerciseScore = 12, subjectId = 4 },
new ExerciseModel { exerciseId = 5, exerciseDate = DateTime.Parse("2016-11-26"), exerciseScore = 10, subjectId = 1 },
};
I was able to successfully group each of them by date and by subject.
var allQuizzes = quizzes.GroupBy(qz => qz.quizDate, (q, values) =>
new
{
Date = q,
Quizzes = values.GroupBy(v => v.subjectId, (c, values2) =>
new {
SubjectId = c,
QuizSum = values2.Sum(v2 => v2.quizScore)
})
});
var allExercises = exercises.GroupBy(ex => ex.exerciseDate, (e, values) =>
new {
Date = e,
Exercises = values.GroupBy(x => x.subjectId, (z, values2) =>
new {
SubjectId = z,
ExerSum = values2.Sum(r => r.exerciseScore)
})
});
var allExams = exams.GroupBy(ex => ex.examDate, (e, values) =>
new
{
Date = e,
Exercises = values.GroupBy(x => x.subjectId, (z, values2) =>
new
{
SubjectId = z,
ExamSum = values2.Sum(r => r.examScore)
})
});
However, I need to join all three of them to get the sum of all scores. The final table should display like this.
-----------------------------------------------------------------
| Date | Math | Science | History | Language |
| 11/25/2016 | 132 | 111 | 0 | 92 |
| 11/26/2016 | 122 | 20 | 0 | 12 |
-----------------------------------------------------------------
I tried to join them, but it can't seem to join by multiple columns.
I select from all 3 collections results in form of the same anonymous class (the same Idea had Andrei in first answer), that allows me just to collect all results together in all list, without mapping and converting.
var allQuiz = quizzes.GroupBy(x => new { x.subjectId, x.quizDate })
.Select(x => new {
Date = x.Key.quizDate,
Subj = x.Key.subjectId,
Sum = x.Sum(r=>r.quizScore)});
var allExam= exams.GroupBy(x => new { x.subjectId, x.examDate })
.Select(x => new {
Date = x.Key.examDate,
Subj = x.Key.subjectId,
Sum = x.Sum(r=>r.examScore)});
var allExc = exercises.GroupBy(x => new { x.subjectId, x.exerciseDate })
.Select(x => new {
Date = x.Key.exerciseDate,
Subj = x.Key.subjectId,
Sum = x.Sum(r=>r.exerciseScore)});
Combining of all results together:
var all = allQuiz.ToList();
all.AddRange(allExam.ToList());
all.AddRange(allExc.ToList());
var result = all.GroupBy(x => new { x.Date, x.Subj })
.Select(x => new { x.Key.Date, x.Key.Subj, Sum = x.Sum(s => s.Sum)});
var list = result.GroupBy(r => r.Date).Select(x => new {
Date = x.Key,
Math = x.SingleOrDefault(t=>t.Subj==1)?.Sum ?? 0,
Science = x.SingleOrDefault(t=>t.Subj==2)?.Sum ?? 0,
History = x.SingleOrDefault(t=>t.Subj==3)?.Sum ?? 0,
Language = x.SingleOrDefault(t=>t.Subj==4)?.Sum ?? 0,
});
Output in LinqPad:
Here is an idea. Instead of keeping the distinction while grouping, you could convert all three to the same structure. For instance:
var allQuizzes = quizzes.GroupBy(qz => qz.quizDate, (q, values) =>
new
{
Date = q,
Results = values.GroupBy(v => v.subjectId, (c, values2) =>
new {
SubjectId = c,
Sum = values2.Sum(v2 => v2.quizScore)
})
});
Notice names "Results" and "Sum" - you can use the same for the other two objects. And now you have three collections, all of the same structure:
{
Date:
Results: [
{SubjectId, Sum}
{SubjectId, Sum}
...
]
}
Since they are all the same now, you can stop treating them differently, use UNION to merge all three, group them by date and within that by subject. Then you could probably iterate through subject list to get necessary info, depends on what you mean by "final table".
This is what i came up with.
It may not be best optimized, but might be enough for you.
I rendered the results into a StringBuilder in my test.
var result =
quizzes.Select(q => new {SubjectId = q.subjectId, Date = q.quizDate, Score = q.quizScore})
.Union(exams.Select(e => new {SubjectId = e.subjectId, Date = e.examDate, Score = e.examScore}))
.Union(exercises.Select(e => new {SubjectId = e.subjectId, Date = e.exerciseDate, Score = e.exerciseScore}))
.GroupBy(arg => arg.Date,
(key, values)=>
new
{
Key = key,
Scores = values.GroupBy(v => v.SubjectId, (s, values2) => new { SubjectId = s, SumScore = values2.Sum(v2 => v2.Score) })
});
StringBuilder sb = new StringBuilder("Date\t\t");
foreach (SubjectModel subject in subjects)
{
sb.Append($"{subject.subjName}\t");
}
sb.AppendLine();
foreach (var record in result)
{
sb.Append($"{record.Key.ToShortDateString()}\t");
foreach (SubjectModel subject in subjects)
{
int sum = record.Scores.Where(s => s.SubjectId == subject.subjId).Select(s => s.SumScore).DefaultIfEmpty(0).Single();
sb.Append($"{sum}\t");
}
sb.AppendLine();
}
string finalTable = sb.ToString();
Instead of using three different anonymous objects to hold the results, make your own class:
public enum TestType
{
Quiz,
Exam,
Exercise,
}
public class TestScore
{
public TestType Type { get; set; }
public DateTime Date { get; set; }
public int Score { get; set; }
public int SubjectId { get; set; }
// Constructors - make a TestScore object
public TestScore(QuizModel q)
{
Type = TestType.Quiz;
Date = q.quizDate;
Score = q.quizScore;
SubjectId = q.SubjectId;
}
public TestScore(ExamModel e)
{
Type = TestType.Exam;
Date = e.examDate;
Score = e.examScore;
SubjectId = e.SubjectId;
}
public TestScore(ExerciseModel e)
{
Type = TestType.Exercise;
Date = e.exerciseDate;
Score = e.exerciseScore;
SubjectId = e.SubjectId;
}
}
Convert to TestScore:
List<TestScore> scores = new List<TestScore>();
scores.AddRange(quizzes.Select(q => new TestScore(q));
scores.AddRange(exams.Select(e => new TestScore(e));
scores.AddRange(exercises.Select(e => new TestScore(e));
Now you have one datasource instead of three, displaying the results becomes easy.
I have a lot of data in the below format..
var data1 = new[] {
new { Product = "Product 1", Year = 2009, Sales = 1212 },
new { Product = "Product 2", Year = 2009, Sales = 522 },
new { Product = "Product 1", Year = 2010, Sales = 1337 },
new { Product = "Product 2", Year = 2011, Sales = 711 },
new { Product = "Product 2", Year = 2012, Sales = 2245 },
new { Product = "Product 3", Year = 2012, Sales = 1000 }
};
If I wanted to get the top 20 rows with max sales, I could do something as below..
data1.OrderByDescending(o=>o.Sales).Take(20);
But what I want to do is get the top 5 Products and (for those products) the top 5 years along with their sales.
So, the output would be something like below:
var outputdata = new[] {
new { Product = "Product 1", Year = 2012, Sales = 2245 },
new { Product = "Product 1", Year = 2010, Sales = 1337 },
new { Product = "Product 1", Year = 2009, Sales = 1212 },
new { Product = "Product 1", Year = 2011, Sales = 711 },
new { Product = "Product 1", Year = 2013, Sales = 522 },
new { Product = "Product 2", Year = 2012, Sales = 1000 }
};
This might be a similar question for sql. but unfortunately could not understand how to convert to linq.
Ok if I understood correctly: First group by the product so you can order by the total sales of a product.
Then you can take only the amount you want. Use SelectMany to flatten the groups:
var data = new[] {
new { Product = "Product 1", Year = 2009, Sales = 1212 },
new { Product = "Product 2", Year = 2009, Sales = 522 },
new { Product = "Product 1", Year = 2010, Sales = 1337 },
new { Product = "Product 2", Year = 2011, Sales = 711 },
new { Product = "Product 2", Year = 2012, Sales = 2245 },
new { Product = "Product 3", Year = 2012, Sales = 1000 }
};
int numberOfProducts = 2;
int numberOfYearsForEachProduct = 3;
var result = data.GroupBy(x => x.Product)
.OrderByDescending(x => x.Sum(y => y.Sales)) //Order products by their total sum of `Sales`
.Take(numberOfProducts )
.SelectMany(x => x.OrderByDescending(y => y.Sales).Take(numberOfYearsForEachProduct)) // Take only the N top years for each product
.ToList();
I used smaller numbers in the Take so I can see that it is doing it correctly
First of all you should get the 20 most sold products
var top20Products = data1
.GroupBy(x => x.Product)
.OrderByDescending(group => group.Sum(x => x.Sales))
.Select(group => group.Key)
.Take(20);
and then select top 5 most sold years of them
var top5yearsOfTop20products = top20Products
.SelectMany(product => data1
.Where(x => x.Product == product)
.OrderByDescending(x => x.Sales)
.Take(5));
If I get you correctly, you want to get top 20 sales for top 5 products.
var ord = data1.OrderByDescending(o => o.Sales)
.Select(o => o.Product)
.Distinct().Take(5);//Get top 5 products by their sales
var salesForTopProducts = data1.OrderByDescending(o => o.Sales)
.Where(o => ord.Contains(o.Product))
.Take(20);//Get top 20 sales for top 5 products
I have a calendar app where you select various combinations of products- a service goes out and gets the available dates based on the calendar date range. A date is only "Available" if ALL selected products are available on a particular date.
class SelectedProduct
{
public int ID { get; set; }
public int Qty { get; set; }
}
class AvailableInventory
{
public int ID { get; set; }
public DateTime Date { get; set; }
}
// List of selected products from user
List<SelectedProduct> SelectedProducts;
// populated from service with all dates for all products
List<AvailableInventory> AvailableInventory;
I want to be able to say get list of Available Inventory for each date that contains inventory for all ID's in selected products.
This is (non-working) pusdo code of a possible solution, I just don't know linq well enough to get it right
var results = List<AvailableInventory>();
foreach (var group in AvailableInventory.GroupBy(x => x.Date))
{
if (group.Contains(ALL ID's in SelectedProducts)
{
results.AddRange(group);
}
}
This groups inventory by date (ignoring the date portion), then selects only those groups that contain all selected product IDs, and finally selects all available inventory for the matching groups.
var results =
AvailableInventory.GroupBy(i => i.Date.Date)
.Where(g => !SelectedProducts.Select(p => p.ID)
.Except(g.Select(i => i.ID))
.Any())
.SelectMany(g => g);
The result is a collection of AvailableInventory.
You can group by the date, then filter out groups that don't have all the SelectedProducts.
// List of selected products from user
List<SelectedProduct> SelectedProducts = new List<SelectedProduct> {
new SelectedProduct { ID = 1, Qty = 1 },
new SelectedProduct { ID = 2, Qty = 2 },
};
// populated from service with all dates for all products
List<AvailableInventory> AvailableInventory = new List<AvailableInventory> {
new AvailableInventory { ID = 1, Date = new DateTime(2014, 04, 11) },
new AvailableInventory { ID = 2, Date = new DateTime(2014, 04, 11) },
new AvailableInventory { ID = 1, Date = new DateTime(2014, 04, 12) },
new AvailableInventory { ID = 2, Date = new DateTime(2014, 04, 13) },
new AvailableInventory { ID = 1, Date = new DateTime(2014, 04, 14) },
new AvailableInventory { ID = 2, Date = new DateTime(2014, 04, 14) },
};
var query = AvailableInventory.GroupBy(i => i.Date)
.Where(g => SelectedProducts.All(s => g.Any(i => i.ID == s.ID)));
foreach(var group in query)
{
Console.WriteLine("Date: {0}", group.Key);
foreach(var inventory in group)
{
Console.WriteLine(" Available: {0}", inventory.ID);
}
}
This would output:
Date: 4/11/2014 12:00:00 AM
Available: 1
Available: 2
Date: 4/14/2014 12:00:00 AM
Available: 1
Available: 2
I think this is what you are looking for. Try this
var result = AvailableInventory.Where(i => SelectedProducts.Any(x => x.ID == i.ID)).GroupBy(o => o.Date)
.Select(g => g.First()).ToList();
This is the test data I used based on your class definition for AvailableInventory and SelectedProduct
// List of selected products from user
List<SelectedProduct> SelectedProducts = new List<SelectedProduct> {
new SelectedProduct { ID = 1, Qty = 2 },
new SelectedProduct { ID = 2, Qty = 4 },
new SelectedProduct { ID = 5, Qty = 10 }
};
// populated from service with all dates for all products
List<AvailableInventory> AvailableInventory = new List<AvailableInventory> {
new AvailableInventory { ID = 1, Date = new DateTime(2014, 04, 01) },
new AvailableInventory { ID = 2, Date = new DateTime(2014, 04, 02) },
new AvailableInventory { ID = 3, Date = new DateTime(2014, 04, 02) },
new AvailableInventory { ID = 4, Date = new DateTime(2014, 04, 10) },
new AvailableInventory { ID = 5, Date = new DateTime(2014, 04, 10) }
};
This should give you only the records with ID = 1, ID = 2 and ID = 5 because that's what common between both AvailableInventory and SelectedProducts lists.
It would help if you actually tried something.
Given this:
List<SelectedProduct> SelectedProducts ;
List<AvailableInventory> AvailableInventory ;
Something like this will probably get what you want:
int[] DatesWithAllSelectedProductsAvailable =
AvailableInventory
.GroupBy( x => x.Date )
.Where ( g => g.All( x => SelectedProducts.Any( p => p.ID == x.ID ) ) )
.Select( x => x.Key )
.Distinct()
.OrderBy( x => x )
.ToArray()
;
How could I select parking with id 1 and only the cars of year 200 in that park
var List<Parking> cityParkings = new List<Parking>
{
new Parking{ id = 1, carsInPark = new List<Car>{ new Car{ year = 2000}, new Car{ year = 2001} }},
new Parking{ id = 2, carsInPark = new List<Car>{ new Car{ year = 2000}, new Car{ year = 1999} }},
new Parking{ id = 3, carsInPark = new List<Car>{ new Car{ year = 2005}, new Car{ year = 2000} }},
}
expected result:
Parking { id = 1, carsInPark = List<Car>{ Car{ year = 2000} } }
select the existing parking instance of id 1 with existing cars instances, but only the cars of 2000's year.
Thanks ;)
Try
cityParkings.Where(p => p.id = 1)
.Select(pp => new Parking(){id = pp.id, carsInPark = pp.carsInPark.Where(c=>c.year == 2000).ToList()});