Reputation: 780
I have now updated to a Complex Situation now and i am terribly stuck. Kindly Let me know if you can share some light to it.
I No more have this as List<string>
. It is now a List<categories>
Which will have the Following properties
public class Categories
{
public string DisplayName { get; set; }
public string ValueCode { get; set; }
public string Count { get; set; }
}
This will have Values like
Category1/SubCategory1
cat1/sc1
5
Category1/SubCategory2
cat1/sc2
4
Category 2/Subcategory1
cat2/sc1
5
Category 2/Subcategory2
cat2/sc2
23
So, i updated my Existing JobCateogry Clas and so on.
public class JobCateogry
{
public string DisplayName { get; set; }
public string ValueCode { get; set; }
public string Count { get; set; }
public List<JobCateogry> SubCategories { get; set; }
}
I tried to Split the string and assign it to the new class in two step first by splitting and then by assiging. But i am sure i am doing it the wrong way, because the moment i split, i loose the count .
var lstCategory = Categories
.Where(i => i.count > 0)
.Select(item => item.valueCode.Split('/')
.Select(k =>(k)).ToList();
List<JobCategories> jobcategories = lstCategory
.Select(item => item.Split(QueryStringConstants.CAT_SEPERATOR.ToCharArray()[0]))
.GroupBy(tokens => tokens[0].Trim(), tokens => tokens[1])
.Select(g => new JobCategories(g.Key, g.DisplayName,g.ToList(),)).ToList();
Can you please help?
(i need to split by Value code and Show the DisplayName and Count)
* Old Question * I have a List that has strings something like this
"Category1/SubType1"
"Category1/SubType2"
"Category1/SubType3"
"Category1/SubType4"
"Category2/SubType1"
"Category2/SubType2"
"Category2/SubType3"
"Category2/SubType4"
i have a class that has two properties.
public string Category { get; set; }
public List<string> SubCategory{ get; set; }
I need to iterate through the list, and pick all the categories and then add subcategories to them.
I had sorted the list and I was able to achieve the traditional way by doing a for loop by taking the first string and comparing with the rest. I was wondering if there is any neater way to do this?
Upvotes: 1
Views: 149
Reputation: 74385
Something like this ought to work
IEnumerable<string> items = GetSomeItems() ;
Widget[] list = items
.Select( s => s.Split('/') )
.GroupBy( x => x[0] , x => x[1] )
.Select( g => new Widget( g.Key , g.ToList() ) )
.ToArray()
;
given a class like this:
class Widget
{
public Widget ( string category , IEnumerable<string> subcategories )
{
this.Category = category ;
this.Subcategories = subcategories.ToArray() ;
}
public string Category { get; private set; }
public string[] Subcategories { get ; private set ; }
}
Upvotes: 0
Reputation: 56726
Splitting each list item and then grouping by category name should give the desired result:
list.Select(item => item.Split('/'))
.GroupBy(tokens => tokens[0], tokens => tokens[1])
.Select(g => new TheClass
{
Category = g.Key,
SubCategory = g.ToList()
});
Upvotes: 2
Reputation: 96557
You can try this approach:
var query = from input in inputs
let split = input.Split('/')
group split[1] by split[0] into grouping
select new MyClass
{
Category = grouping.Key,
SubCategory = grouping.ToList()
};
Upvotes: 0