patterncsharpMinor
From a list of lists to a single object
Viewed 0 times
objectsinglelistslistfrom
Problem
Long story short a user can have things in common with another user, but I don't what to show more than 4 main categories in common, only the first 4, the priority is the order in which the lists are added.
Below is my current approach, but I feel that this can be simplified, and maybe improve the time.
Any thoughts?
```
var user1 = await UserService.GetInfo(id, reqUserId);
var user2 = await UserInfoService.GetInfo(reqUserId, reqUserId);
// determine info
List infos = new List();
InfoVM info = new InfoVM();
infos.Add( new InfoItemVM { Name = "Projects", Data = user1.Projects.Intersect(user2.Projects).ToList() });
infos.Add( new InfoItemVM { Name = "Companies", Data = user1.Companies.Intersect(user2.Companies).ToList() });
infos.Add( new InfoItemVM { Name = "Schools", Data = user1.Schools.Intersect(user2.Schools).ToList() });
infos.Add( new InfoItemVM { Name = "Hobbies", Data = user1.Hobbies.Intersect(user2.Hobbies).ToList() });
infos.Add( new InfoItemVM { Name = "Locations", Data = user1.Locations.Intersect(user2.Locations).ToList() });
infos.Add( new InfoItemVM { Name = "Interests", Data = user1.Interests.Intersect(user2.Interests).ToList() });
infos.Add( new InfoItemVM { Name = "Stuff", Data = user1.Stuff.Intersect(user2.Stuff).ToList() });
infos.Add( new InfoItemVM { Name = "Things", Data = user1.Things.Intersect(user2.Things).ToList() });
infos.Add( new InfoItemVM { Name = "MyLists", Data = user1.MyLists.Intersect(user2.MyLists).ToList() });
// assign bullet classes to maximum 4 in info categories
int max = 0; int med = 0; int medsm = 0; int min = 0;
int count = 0;
foreach (var c in infos.Where(c => c.Data.Count != 0))
{
count++;
if (count > 4 && c.Data.Count != 0)
c.Data.Clear();
}
foreach (var c in infos)
{
int value = c.Data.Count;
if (value > max)
{
med = max;
medsm = max;
min = max;
max = value;
}
else if (value med)
{
medsm = med;
min = med;
med = value
Below is my current approach, but I feel that this can be simplified, and maybe improve the time.
Any thoughts?
```
var user1 = await UserService.GetInfo(id, reqUserId);
var user2 = await UserInfoService.GetInfo(reqUserId, reqUserId);
// determine info
List infos = new List();
InfoVM info = new InfoVM();
infos.Add( new InfoItemVM { Name = "Projects", Data = user1.Projects.Intersect(user2.Projects).ToList() });
infos.Add( new InfoItemVM { Name = "Companies", Data = user1.Companies.Intersect(user2.Companies).ToList() });
infos.Add( new InfoItemVM { Name = "Schools", Data = user1.Schools.Intersect(user2.Schools).ToList() });
infos.Add( new InfoItemVM { Name = "Hobbies", Data = user1.Hobbies.Intersect(user2.Hobbies).ToList() });
infos.Add( new InfoItemVM { Name = "Locations", Data = user1.Locations.Intersect(user2.Locations).ToList() });
infos.Add( new InfoItemVM { Name = "Interests", Data = user1.Interests.Intersect(user2.Interests).ToList() });
infos.Add( new InfoItemVM { Name = "Stuff", Data = user1.Stuff.Intersect(user2.Stuff).ToList() });
infos.Add( new InfoItemVM { Name = "Things", Data = user1.Things.Intersect(user2.Things).ToList() });
infos.Add( new InfoItemVM { Name = "MyLists", Data = user1.MyLists.Intersect(user2.MyLists).ToList() });
// assign bullet classes to maximum 4 in info categories
int max = 0; int med = 0; int medsm = 0; int min = 0;
int count = 0;
foreach (var c in infos.Where(c => c.Data.Count != 0))
{
count++;
if (count > 4 && c.Data.Count != 0)
c.Data.Clear();
}
foreach (var c in infos)
{
int value = c.Data.Count;
if (value > max)
{
med = max;
medsm = max;
min = max;
max = value;
}
else if (value med)
{
medsm = med;
min = med;
med = value
Solution
You should replace individual properties on
where
InfoVM with a collection. Then you can use LINQ to build it:var commonCategories = infos.Where(x => x.Data.Count > 0)
.Take(4)
.OrderBy(x => x.Data.Count);
var index = 0;
foreach(var category in commonCategories)
{
category.Bullet = (BulletSize) index++;
info.CommonCategories .Add(category);
}where
enum BulletSize
{
Small,
MediumSmall,
Medium,
Large
}
public class InfoVM
{
public ObservableCollection CommonCategories { get; set; }
....
}Code Snippets
var commonCategories = infos.Where(x => x.Data.Count > 0)
.Take(4)
.OrderBy(x => x.Data.Count);
var index = 0;
foreach(var category in commonCategories)
{
category.Bullet = (BulletSize) index++;
info.CommonCategories .Add(category);
}enum BulletSize
{
Small,
MediumSmall,
Medium,
Large
}
public class InfoVM
{
public ObservableCollection<InfoItemVM> CommonCategories { get; set; }
....
}Context
StackExchange Code Review Q#152775, answer score: 8
Revisions (0)
No revisions yet.