Set and Get a collection in C# - c#

I have two classes: Group and Item.
public class Group
{
public string Name{ get; set; }
public List<Item> ItemList { get; set; }
}
And then Item
public class Item
{
public int ID{ get; set; }
public string Name{ get; set; }
public string Description{ get; set; }
public Group ItemGroup {get;set;}
}
Each group show have a set of items.
The following code is meant to get the list of items of a particular group, and it works when the ItemGroup in the Items class is set to type string, but not as a type Group.
public IEnumerable<Item> GetItemByGroup(string group)
{
return repository.GetAllItems().Where(
p => string.Equals(p.ItemGroup, group, StringComparison.OrdinalIgnoreCase));
}
How to change the code to get the list of items in a group by its Name property which is set in the Group class.
And how do I set a list/collection of Items in the Group class

I think this should work. Give it a hit
public IEnumerable<Item> GetItemByGroup(string group)
{
return repository.GetAllItems().Where(p =>p.ItemGroup.Name.Equals(group));
}
Update
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<Item> list = new List<Item>();
Item i = new Item()
{
ID = 1,
Name = "Amit",
Description = "Test",
ItemGroup = new Group() { Name = "A" }
};
list.Add(i);
i = new Item()
{
ID = 2,
Name = "Amit1",
Description = "Test1",
ItemGroup = new Group() { Name = "A1" }
};
list.Add(i);
i = new Item()
{
ID = 3,
Name = "Amit11",
Description = "Test11",
ItemGroup = new Group() { Name = "A11" }
};
list.Add(i);
i = new Item()
{
ID = 4,
Name = "Amit111",
Description = "Test111",
ItemGroup = new Group() { Name = "A111" }
};
list.Add(i);
i = new Item()
{
ID = 9,
Name = "Amit4a",
Description = "Test4a",
ItemGroup = new Group() { Name = "A111" }
};
list.Add(i);
i = new Item()
{
ID = 5,
Name = "Amit5",
Description = "Test5",
ItemGroup = new Group() { Name = "A111" }
};
list.Add(i);
i = new Item()
{
ID = 6,
Name = "Amit6",
Description = "Test6",
ItemGroup = new Group() { Name = "A111" }
};
list.Add(i);
var list1 = list.Where(p => p.ItemGroup.Name.Equals("A111"));
// Console.Write(list1.Count());
foreach (var item in list1)
{
Console.WriteLine(string.Format("ID: {0} Name: {1} Description: {2} Group: {3}",item.ID,item.Name,item.Description,item.ItemGroup.Name));
}
Console.Read();
}
}
public class Group
{
public string Name { get; set; }
}
public class Item
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Group ItemGroup { get; set; }
}
}

Related

flat view model incorrect output

This model I would like flat into a view model but the Current Output is not correct it is setting all the ChildProductId to the last value. I am creating one List<SubscriptionViewModel> and then creating multiple SubscriptionViewModel and adding to the collection.
List<Subscription> ListOfSubscriptions = new List<Subscription>();
List<SubscriptionChild> SubscriptionChild = new List<SubscriptionChild>();
SubscriptionChild.Add(new SubscriptionChild() { ChildProductId = 1, ChildProductName = "Child 1" });
SubscriptionChild.Add(new SubscriptionChild() { ChildProductId = 2, ChildProductName = "Child 2" });
ListOfSubscriptions.Add(new Subscription() { SubscriptionId = 1, ParentProductId=1, ParentProductName = "Product 1",ListOfSubscriptionChild= SubscriptionChild });
SubscriptionChild.Clear();
ListOfSubscriptions.Add(new Subscription() { SubscriptionId = 2, ParentProductId = 2, ParentProductName = "Product 2"});
SubscriptionChild.Clear();
SubscriptionChild.Add(new SubscriptionChild() { ChildProductId = 3, ChildProductName = "Child 3" });
SubscriptionChild.Add(new SubscriptionChild() { ChildProductId = 4, ChildProductName = "Child 4" });
ListOfSubscriptions.Add(new Subscription() { SubscriptionId = 3, ParentProductId = 3, ParentProductName = "Product 3", ListOfSubscriptionChild = SubscriptionChild });
List<SubscriptionViewModel> SubscriptionViewModel = new List<SubscriptionViewModel>();
foreach (var Subscription in ListOfSubscriptions)
{
SubscriptionViewModel vm = new SubscriptionViewModel();
vm.SubscriptionId = Subscription.SubscriptionId;
vm.ParentProductId = Subscription.ParentProductId;
vm.ParentProductName = Subscription.ParentProductName;
int count = Subscription.ListOfSubscriptionChild == null ? 0 : Subscription.ListOfSubscriptionChild.Count;
if (count == 0) {
SubscriptionViewModel.Add(vm);
}
else
{
var listOfChild = Subscription.ListOfSubscriptionChild.ToList();
foreach (var item in listOfChild)
{
vm.ChildProductId = item.ChildProductId;
vm.ChildProductName = item.ChildProductName;
SubscriptionViewModel.Add(vm);
}
}
}
foreach (var item in SubscriptionViewModel)
{
Console.WriteLine(string.Format("SubscriptionId{0} ParentProductId-{1} ChildProductId-{2}", item.SubscriptionId, item.ParentProductId, item.ChildProductId));
}
class Subscription
{
public int SubscriptionId { get; set; }
public int ParentProductId { get; set; }
public string ParentProductName { get; set; }
public List<SubscriptionChild> ListOfSubscriptionChild { get; set; }
}
class SubscriptionChild
{
public string ChildProductName { get; set; }
public int ChildProductId { get; set; }
}
class SubscriptionViewModel
{
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; }
}
Current Output
SubscriptionId1 ParentProductId-1 ChildProductId-4
SubscriptionId1 ParentProductId-1 ChildProductId-4
SubscriptionId2 ParentProductId-2 ChildProductId-0
SubscriptionId3 ParentProductId-3 ChildProductId-4
SubscriptionId3 ParentProductId-3 ChildProductId-4
expected outcome
SubscriptionId1 ParentProductId-1 ChildProductId-1
SubscriptionId1 ParentProductId-1 ChildProductId-2
SubscriptionId2 ParentProductId-2 ChildProductId-0
SubscriptionId3 ParentProductId-3 ChildProductId-3
SubscriptionId3 ParentProductId-3 ChildProductId-4
You're overwriting your SubscriptionViewModel for all of your children. You need to create a new one for each child:
foreach (var Subscription in ListOfSubscriptions)
{
int count = Subscription.ListOfSubscriptionChild == null ? 0 : Subscription.ListOfSubscriptionChild.Count;
if (count == 0)
{
SubscriptionViewModel vm = new SubscriptionViewModel();
vm.SubscriptionId = Subscription.SubscriptionId;
vm.ParentProductId = Subscription.ParentProductId;
vm.ParentProductName = Subscription.ParentProductName;
SubscriptionViewModel.Add(vm);
}
else
{
var listOfChild = Subscription.ListOfSubscriptionChild.ToList();
foreach (var item in listOfChild)
{
// Instantiate a new model for each child
SubscriptionViewModel vm = new SubscriptionViewModel();
vm.SubscriptionId = Subscription.SubscriptionId;
vm.ParentProductId = Subscription.ParentProductId;
vm.ParentProductName = Subscription.ParentProductName;
vm.ChildProductId = item.ChildProductId;
vm.ChildProductName = item.ChildProductName;
SubscriptionViewModel.Add(vm);
}
}
}

Query mongo document array

I have the next mongo document structure :
_id
-countryCode
-keywordID
-name
-displayName
-categories:[Array]
-_id
-name
-position
-canonical
I would like to get all the keywords that are in a specific category only knowing the category's ID. I am using the mongo C# driver but don't know how could I check what's inside that array.
I would like to send a list with the category ID's and get back all the keywords that have a category from that list.
public async Task<List<Keyword>> GetKeywords(List<long> keywordCatIds, string countryCode)
{
var mongoCollection = MongoDatabase.GetCollection<Keyword>("Keywords");
try
{
FilterDefinition<Keyword> mongoFilter = Builders<Keyword>.Filter.In(c=>c.Categories, keywordCatIds);
return await mongoCollection.Find(mongoFilter,null).ToListAsync<Keyword>();
}
catch (Exception ex)
{
Logger.Error(ex, "Multiple ids for Country Code: {0}, ids: {1}", countryCode, string.Join(',', keywordCatIds.Select(s => s)));
return null;
}
}
Your In function looks like a "categories._id" filter in normal mongoDB. Which transitions into an ElemMatch. I created a project which fills the db, than selects
all the keywords that are in a specific category only knowing the category's ID
public class CustomID
{
public string CountryCode { get; set; }
public long KeywordId { get; set; }
public string Name { get; set; }
}
public class Keyword
{
[BsonId]
public CustomID Id { get; set; }
public List<Category> Categories { get; set; }
}
public class Category
{
[BsonId]
public long Id { get; set; }
public string Name { get; set; }
public int Position { get; set; }
}
internal class Program
{
public static IMongoDatabase MongoDatabase { get; private set; }
public static async Task Main()
{
var conventionPack = new ConventionPack
{
new CamelCaseElementNameConvention()
};
ConventionRegistry.Register(
"CustomConventionPack",
conventionPack,
t => true);
var client = new MongoClient();
MongoDatabase = client.GetDatabase("SO");
var ret = await GetKeywords(new List<long> {1L, 2L}, "HU-hu");
// ret is A and B. C is filtered out because no category id of 1L or 2L, D is not HU-hu
}
public static async Task<List<Keyword>> GetKeywords(List<long> keywordCatIds, string countryCode)
{
var mongoCollection = MongoDatabase.GetCollection<Keyword>("keywords");
// be ware! removes all elements. For debug purposes uncomment>
//await mongoCollection.DeleteManyAsync(FilterDefinition<Keyword>.Empty);
await mongoCollection.InsertManyAsync(new[]
{
new Keyword
{
Categories = new List<Category>
{
new Category {Id = 1L, Name = "CatA", Position = 1},
new Category {Id = 3L, Name = "CatC", Position = 3}
},
Id = new CustomID
{
CountryCode = "HU-hu",
KeywordId = 1,
Name = "A"
}
},
new Keyword
{
Categories = new List<Category>
{
new Category {Id = 2L, Name = "CatB", Position = 2}
},
Id = new CustomID
{
CountryCode = "HU-hu",
KeywordId = 2,
Name = "B"
}
},
new Keyword
{
Categories = new List<Category>
{
new Category {Id = 3L, Name = "CatB", Position = 2}
},
Id = new CustomID
{
CountryCode = "HU-hu",
KeywordId = 3,
Name = "C"
}
},
new Keyword
{
Categories = new List<Category>
{
new Category {Id = 1L, Name = "CatA", Position = 1}
},
Id = new CustomID
{
CountryCode = "EN-en",
KeywordId = 1,
Name = "EN-A"
}
}
});
var keywordFilter = Builders<Keyword>.Filter;
var categoryFilter = Builders<Category>.Filter;
var mongoFilter =
keywordFilter.ElemMatch(k => k.Categories, categoryFilter.In(c => c.Id, keywordCatIds)) &
keywordFilter.Eq(k => k.Id.CountryCode, countryCode);
return await mongoCollection.Find(mongoFilter).ToListAsync();
}
}

Linq return parent objects that have child items matching ALL items in separate list

I have an object which has a variable length list of items (incomingList in code example) and a list of people which each have a list of items. I want to return only those people that have all the items in the incomingList.
So looking at the example, I only want person 1 and 3 returned.
The people are in a database and I want to retrieve as little data as possible so I am trying to work out what the linq query needs to be to achieve this? If I knew the length of the incomingList was always going to be the same I could do "...Any(..) && ...Any(...) &&" etc - but the length will vary.
void Main()
{
var incomingList = new IncomingItem();
var matchItem1 = new MatchItem { ItemType = "objectId", ItemValue = "60" };
var matchItem2 = new MatchItem { ItemType = "area", ItemValue = "CU" };
incomingList.MatchList = new List<MatchItem>();
incomingList.MatchList.Add(matchItem1);
incomingList.MatchList.Add(matchItem2);
var people = new List<Person>();
var person1 = new Person { Id = 1 };
person1.ListOfItems = new List<Item>();
person1.ListOfItems.Add(new Item { ItemType = "objectId", ItemValue = "60" });
person1.ListOfItems.Add(new Item { ItemType = "objectId", ItemValue = "1" });
person1.ListOfItems.Add(new Item { ItemType = "objectId", ItemValue = "30" });
person1.ListOfItems.Add(new Item { ItemType = "area", ItemValue = "CO" });
person1.ListOfItems.Add(new Item { ItemType = "area", ItemValue = "CU" });
people.Add(person1);
var person2 = new Person { Id = 2 };
person2.ListOfItems = new List<Item>();
person2.ListOfItems.Add(new Item { ItemType = "objectId", ItemValue = "60" });
people.Add(person2);
var person3 = new Person { Id = 3 };
person3.ListOfItems = new List<Item>();
person3.ListOfItems.Add(new Item { ItemType = "objectId", ItemValue = "60" });
person3.ListOfItems.Add(new Item { ItemType = "area", ItemValue = "CU" });
people.Add(person3);
var person4 = new Person { Id = 4 };
person4.ListOfItems = new List<Item>();
person4.ListOfItems.Add(new Item { ItemType = "objectId", ItemValue = "12" });
people.Add(person4);
}
public class IncomingItem
{
public IList<MatchItem> MatchList { get; set; }
}
public class MatchItem
{
public List<object> SomeMoreInformation { get; set; }
public string ItemType { get; set; }
public string ItemValue { get; set; }
}
public class Person
{
public int Id { get; set; }
public IList<Item> ListOfItems { get; set; }
}
public class Item
{
public int Id { get; set; }
public int PersonId { get; set; }
public string ItemType { get; set; }
public string ItemValue { get; set; }
}
This returns all people that have all items of incomingList in their ListOfItems:
var result = people.Where(p => incomingList.MatchList
.All(l => p.ListOfItems.Select(loi => new { loi.ItemType, loi.ItemValue })
.Contains(new { l.ItemType, l.ItemValue }) ));
Anonymous types should have properties with equal names and types to resolve to "equal", which condition is met in this case.

How to map nested Property with Automapper

I am trying to map Student with StudentDto, this is what I am doing but it is complaining about the nested property which is of type List<StudentContact>
Both the objects, StudentDto and Student have exactly the same properties, this is what i am using to try to map the objects.
var config = new MapperConfiguration(
cfg => cfg.CreateMap<StudentDto, Student>());
var mapper = config.CreateMapper();
var driverActivationResponse = mapper.Map <List<Student> > (studentDto);// "studentDto" is List<StudentDto>
my classes
public class StudentDto
{
public StudentDto()
{
if(StudentContacts==null) StudentContacts=new List<StudentContact>();
}
public string Id { get; set; }
public List<StudentContact> StudentContacts { get; set; }
}
public class Student
{
public Student()
{
if(StudentContacts==null) StudentContacts=new List<StudentContact>();
}
public string Id { get; set; }
public List<StudentContact> StudentContacts { get; set; }
}
public class StudentContact
{
public string ContactName { get; set; }
public string PrimaryContactNo { get; set; }
}
This should help -
AutoMapper.Mapper.CreateMap<Student, StudentDto>()
.ForMember(a => a.StudentContacts, b => b.ResolveUsing(c => c.StudentContacts));
var map = Mapper.Map<StudentDto>(new Student
{
Id = "100",
StudentContacts = new List<StudentContact>
{
new StudentContact{ContactName = "test",PrimaryContactNo = "tset"}
}
});
you cannot map like mapper.Map <List<Student>>(studentDto);. The top level member cannot be a list when using automapper.
Does it help to specify the source collection type and destination collection type in your Map call?
var driverActivationResponse = mapper.Map<List<Student>, List<StudentDto>>(studentDto);
It looks like the AutoMapper code you have is correct. If you're still getting an error, something else must be wrong. Perhaps your studentDto is not really a List<StudentDto>?
In any case, here's an entire example that works without error:
using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
namespace ConsoleSandbox
{
class Program
{
public static void Main()
{
var config = new MapperConfiguration(
cfg => cfg.CreateMap<StudentDto, Student>());
var mapper = config.CreateMapper();
var studentDtos = new[]
{
new StudentDto
{
Id = "1",
StudentContacts = new[]
{
new StudentContact { ContactName = "Dan", PrimaryContactNo = "123" },
new StudentContact { ContactName = "Stan", PrimaryContactNo = "456" },
}.ToList()
},
new StudentDto
{
Id = "2",
StudentContacts = new[]
{
new StudentContact { ContactName = "Foo", PrimaryContactNo = "789" },
new StudentContact { ContactName = "Bar", PrimaryContactNo = "101112" },
}.ToList()
},
}.ToList();
var driverActivationResponse = mapper.Map<List<Student>>(studentDtos);
Console.WriteLine($"Contacts Count: {driverActivationResponse.Count}");
Console.ReadKey();
}
}
public class StudentDto
{
public string Id { get; set; }
public List<StudentContact> StudentContacts { get; set; }
public StudentDto()
{
if (StudentContacts == null) StudentContacts = new List<StudentContact>();
}
}
public class Student
{
public string Id { get; set; }
public List<StudentContact> StudentContacts { get; set; }
public Student()
{
if (StudentContacts == null) StudentContacts = new List<StudentContact>();
}
}
public class StudentContact
{
public string ContactName { get; set; }
public string PrimaryContactNo { get; set; }
}
}

Cannot implicitly convert type 'System.Web.Mvc.SelectList' to 'System.Collections.Generic.ICollection<System.Web.Mvc.SelectList>'

I am trying to add some values into my SelectList data member in my object but I get an error
public ActionResult Create()
{
var paf = new ProductAddForm();
paf.Sizes = new SelectList(m.GetProductSizes());
paf.Suppliers = new SelectList(m.GetAllSuppliersList(), "Id", "Name");
return View(paf);
}
that is my creat function, and the paf.Sizes / paf.Suppliers code does not work.
My productaddform class:
public class ProductAddForm
{
public double MSRP { get; set; }
public string Name { get; set; }
public string ProductId { get; set; }
public ICollection<SelectList> Sizes { get; set; }
public ICollection<SelectList> Suppliers { get; set; }
public string UPC { get; set; }
}
And my methods in my manager.cs
public IEnumerable<SupplierList> GetAllSuppliersList()
{
var fetchedObjects = ds.Suppliers.OrderBy(n => n.Name);
var Suppliers = new List<SupplierList>();
foreach (var item in fetchedObjects)
{
var s = new SupplierList();
s.Name = item.Name;
s.Id = item.Id;
Suppliers.Add(s);
}
return (Suppliers);
}
public List<string> GetProductSizes()
{
return new List<string>() { "Small", "Medium", "Large" };
}
Whats wrong?
Suppliers is a collection of SelectList. So you need to Add item into the collection
Change
paf.Suppliers = new SelectList(m.GetAllSuppliersList(), "Id", "Name");
to
paf.Suppliers.Add(new SelectList(m.GetAllSuppliersList(), "Id", "Name"));

Categories

Resources