Hi,
I have to extend a type to add an extra property then convert back to the original type before inserting into the database. I have this working but my solution seems like a hack and there must be a better way. Here's what i have:
public class Item
{
public string Property1 { get; set; }
public string Property2 { get; set; }
...
}
public class SelectedItem : Item
{
public bool IsSelected { get; set; }
public SelectedItem(Item item)
{
Property1 = item.Property1;
Property2 = item.Property2;
...
IsSelected = truel
}
}
Now i can take a list of items and convert them to a list of SelectedItem by saying:
var selectedItems = items.Select(i => new SelectedItem(i)).ToList();
Is not there not a way to remove the constructor since it inherits from Item anyway.
I then have the reverse problem i need to loop over the selected items and insert any in to the database that are selected (IsSelected = true). Here's my code so far:
foreach (var selectedItem in selectedItems)
{
if (selectedItem.IsSelected)
_repository<Item>().Insert(new Item() { Property1 = selectedItem.Property1, Property2 = selectedItem.Property2 ... });
}
Again this seems somewhat of a hack since it has to create a new Item setting each property. I tried doing:
foreach (var selectedItem in selectedItems)
{
if (selectedItem.IsSelected)
_repository<Item>().Insert(selectedItem);
}
While this compiles my application throws an error on Insert that it would like an item of type Item and not SelectedItem.
Appreciate your support.
I have to extend a type to add an extra property then convert back to the original type before inserting into the database. I have this working but my solution seems like a hack and there must be a better way. Here's what i have:
public class Item
{
public string Property1 { get; set; }
public string Property2 { get; set; }
...
}
public class SelectedItem : Item
{
public bool IsSelected { get; set; }
public SelectedItem(Item item)
{
Property1 = item.Property1;
Property2 = item.Property2;
...
IsSelected = truel
}
}
Now i can take a list of items and convert them to a list of SelectedItem by saying:
var selectedItems = items.Select(i => new SelectedItem(i)).ToList();
Is not there not a way to remove the constructor since it inherits from Item anyway.
I then have the reverse problem i need to loop over the selected items and insert any in to the database that are selected (IsSelected = true). Here's my code so far:
foreach (var selectedItem in selectedItems)
{
if (selectedItem.IsSelected)
_repository<Item>().Insert(new Item() { Property1 = selectedItem.Property1, Property2 = selectedItem.Property2 ... });
}
Again this seems somewhat of a hack since it has to create a new Item setting each property. I tried doing:
foreach (var selectedItem in selectedItems)
{
if (selectedItem.IsSelected)
_repository<Item>().Insert(selectedItem);
}
While this compiles my application throws an error on Insert that it would like an item of type Item and not SelectedItem.
Appreciate your support.