Linq拆分类的属性,并将其分配给另一个自定义类 [英] Linq Split properties of Class and assign it to another Custom Class
问题描述
我现在的处境很复杂,我陷入了困境.请告诉我是否可以分享一些灯光.
I have a Complex Situation now and i am terribly stuck. Kindly Let me know if you can share some light to it.
我有一个
将具有以下属性的列表
public class Categories
{
public string DisplayName { get; set; }
public string ValueCode { get; set; }
public string Count { get; set; }
}
这将具有类似
Category1/SubCategory1
cat1/sc1
5
Category1/SubCategory2
cat1/sc2
4
Category 2/Subcategory1
cat2/sc1
5
Category 2/Subcategory2
cat2/sc2
23
我创建了一个自定义类来填充值
I created a Custom Class to fill in the values
public class JobCateogry
{
public string DisplayName { get; set; }
public string ValueCode { get; set; }
public string Count { get; set; }
public List<JobCateogry> SubCategories { get; set; }
}
我必须在代码值中分割字符串并将其分配给SubCategory.
I have to Split the String in the Code Value and assign it to the SubCategory.
就像我的最终失业"类别一样
Like My Final out of jobCategory would be
- 类别1
- Cat1
- 9
- SubCategory1
- sub1
- 5
- SubCateogry2
- sub2
- 4
- Category1
- Cat1
- 9
- SubCategory1
- sub1
- 5
- SubCateogry2
- sub2
- 4
我试图拆分字符串并将其分配给新类,分两步,首先是拆分,然后是辅助.但是我确定我做错了方向,因为一分手,我就失去了计数.
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();
可以帮忙吗?
推荐答案
有点奇怪的任务
这可能不是最好的解决方案,它仅适用于两层:-),并且我尝试保持很多linq的乐趣
It might not be the best solution and it only works with the two layers :-), and i tried keeping a lot of linq for the fun of it
无论如何希望它能使您前进.
anyway hope it can get you moving forward.
完整代码段 https://gist.github.com/cbpetersen/db698def9a04ebb2abbc >
static void Main(string[] args) { var cats = new[] { new Categories { Count = "5", ValueCode = "cat1/sc1", DisplayName = "Category1/SubCategory1" }, new Categories { Count = "4", ValueCode = "cat1/sc2", DisplayName = "Category1/SubCategory2" }, new Categories { Count = "5", ValueCode = "cat2/sc1", DisplayName = "Category2/Subcategory1" }, new Categories { Count = "23", ValueCode = "cat2/sc2", DisplayName = "Category2/Subcategory2" } }; var categories = cats.Select(x => x.DisplayName.Split('/')[0]).Distinct(); var list = new List<JobCateogries>(); foreach (var category in categories) { var a = new JobCateogries { ValueCode = cats.Where(x => x.DisplayName.Split('/')[0] == category) .Select(x => x.ValueCode.Split('/')[0]).FirstOrDefault(), DisplayName = category, SubCategories = cats.Where(x => x.DisplayName.Split('/')[0] == category) .Select(x => new JobCateogries { SubCategories = new List<JobCateogries>(), Count = x.Count, DisplayName = x.DisplayName.Split('/')[1], ValueCode = x.ValueCode.Split('/')[1] }).ToList(), }; a.Count = a.SubCategories.Select(x => int.Parse(x.Count)).Sum().ToString(); list.Add(a); } list.ForEach(x => Print(x)); Console.ReadKey(); } public static void Print(JobCateogries category, int indent = 0) { var prefix = string.Empty.PadLeft(indent); Console.WriteLine(prefix + category.DisplayName); Console.WriteLine(prefix + category.ValueCode); Console.WriteLine(prefix + category.Count); category.SubCategories.ForEach(x => Print(x, indent + 4)); }
这篇关于Linq拆分类的属性,并将其分配给另一个自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!