I am trying to build this view model that has a ChildViewModel and this was the only way I was able to get it working. Any advice on how to make this code; cleaner, more effective, just overall better! It’s just the adding to view model that will need to be reviewed the other section is to create the mock up.
static void Main(string[] args)
{
try
{
//Setup Database object
List<Subscription> ListOfSubscription = new List<Subscription>();
ListOfSubscription.Add(new Subscription() { SubscriptionId = 1, ParentProductId = 4, ChildProductId = 4 , ParentProductName = "Product 1", ChildProductName = "Product 1", GroupId = 362 });
ListOfSubscription.Add(new Subscription() { SubscriptionId = 2, ParentProductId = 114, ChildProductId = 1, ParentProductName = "Product 2", ChildProductName = "Product 3", GroupId = 1 });
ListOfSubscription.Add(new Subscription() { SubscriptionId = 3, ParentProductId = 114, ChildProductId = 2, ParentProductName = "Product 2", ChildProductName = "Product 4",GroupId = 1 });
//Review Section of the Code
var groupedListOfSubscription = ListOfSubscription.GroupBy(u => u.GroupId).ToList();
List<SubscriptionViewModel> SubscriptionViewModel = new List<SubscriptionViewModel>();
foreach (var record in groupedListOfSubscription)
{
int groupId = record.Key;
var SelectListofSubscription = ListOfSubscription.Where(w=> w.GroupId == groupId).ToList();
foreach (var subscription in SelectListofSubscription)
{
SubscriptionViewModel svm = new SubscriptionViewModel();
if (subscription.ParentProductId == subscription.ChildProductId)
{
svm.ParentProductId = subscription.ParentProductId;
svm.SubscriptionIds = subscription.SubscriptionId.ToString();
svm.GroupId = subscription.GroupId;
svm.ParentProductName = subscription.ParentProductName;
SubscriptionViewModel.Add(svm);
}
else
{
svm.ParentProductId = subscription.ParentProductId;
svm.GroupId = subscription.GroupId;
svm.ParentProductName = subscription.ParentProductName;
var SelectChildListofSubscription = ListOfSubscription.Where(w => w.GroupId == groupId).ToList();
StringBuilder builderSubscriptionIds = new StringBuilder();
List<SubscriptionChildViewModel> _scvm = new List<SubscriptionChildViewModel>();
foreach (var child in SelectChildListofSubscription)
{
builderSubscriptionIds.Append(child.ChildProductName);
builderSubscriptionIds.Append(",");
_scvm.Add(new SubscriptionChildViewModel()
{
ChildProductId = child.ChildProductId,
ChildProductName = child.ChildProductName
});
}
svm.ListOfSubscriptionChild = _scvm;
svm.SubscriptionIds = builderSubscriptionIds.ToString();
SubscriptionViewModel.Add(svm);
}
}
}
}
catch (Exception ex)
{
ex.ToString();
}
}
class Subscription
{
public int SubscriptionId { get; set; }
public int ParentProductId { get; set; }
public string ParentProductName { get; set; }
public string ChildProductName { get; set; }
public int ChildProductId { get; set; }
public int GroupId { get; set; }
public DateTime? EndDate { get; set; }
}
class SubscriptionViewModel
{
public int SubscriptionId { get; set; }
public int ParentProductId { get; set; }
public string ParentProductName{ get; set; }
public string SubscriptionIds { get; set; }
public int GroupId { get; set; }
public List<SubscriptionChildViewModel> ListOfSubscriptionChild { get; set; }
}
class SubscriptionChildViewModel
{
public string ChildProductName { get; set; }
public int ChildProductId { get; set; }
}
Output
<Subscription>
<Parent>Product 1</Parent>
<ProductId>4</ProductId>
<SubscriptionIds Ids="1" />
</Subscription>
<Subscription>
<Parent>Product 2</Parent>
<ProductId>114</ProductId>
<SubscriptionIds Ids="2,3" />
<Children>
<Child Name="Product 3">
<ChildProductId>1</ChildProductId>
</Child>
<Child Name="Product 4">
<ChildProductId>2</ChildProductId>
</Child>
</Children>
</Subscription>
ListOfSubscription
is naming in the style of Hungarian notation at its worst. I would give thesubscriptions
name (in the plural). \$\endgroup\$try
covering all of it because functionality is shotgunned all over that block, ... all are symptoms of the foundational design. Code Review rules ask for complete, working code postings so accurate assessments can be made. No code can live in a vacuum. \$\endgroup\$SubscriptionViewModel
s withSubscriptionId
= 0 and the latter two are identical (havingProduct 3,Product 4,
). It would help to see your expected output. \$\endgroup\$