c# - How to map data to class structure using linq? -
i have raw data separated comma like:
name value image catid id blue, era , colors/col_image ,37 , 1 pink, aka, colors/col_image ,37 , 2
and prepared class structure :
public class dailystuffs { public string stuffname { get; set; } public string convertedname { get; set; } public string stuffimage { get; set; } public int categoryid { get; set; } } public class stufftype { public string category { get; set; } public list<dailystuffs> dailystuffs; }
i want group item according category(color,birds etc) . tried using following code :
list<stufftype> stuff = (from line in lines let data = line.split(',').tolist() select new stufftype { category = definerange(convert.toint16(data[4])), dailystuffs = (from dat in data select new dailystuffs { stuffname = data[1] }).tolist() }).tolist();
and definerange returning string value :
private string definerange(int value) { if (value >= 1 && value <= 10) { return "colors"; }}
i getting wrong list category part of each item.
any suggestion guys? how should approach this?
it seems have errors in code. category column data[3]
, name column data[0]
. closest linq can work design is:
var stuff = (from line in lines let data = line.split(',').tolist() group data data[3] c select new stufftype { category = definerange(convert.toint32(c.key)), dailystuffs = (from s in c select new dailystuffs { stuffname = s[0], convertedname = s[1], stuffimage = s[2], categoryid = convert.toint32(s[3]) }).tolist() }).tolist();
of course have fix definerange()
function work catid
column. here fiddle. entered more data testing.
Comments
Post a Comment