public class section { public string Group { get; set; } public string Order { get; set; } public string Section { get; set; } public string Title { get; set; } } static void Main(string[] args) { var sec = new List<section>(); sec.Add(new section { Group = "1", Order = "1", Section = "1.1", Title = "one one" }); sec.Add(new section { Group = "1", Order = "3", Section = "1.3", Title = "one three" }); sec.Add(new section { Group = "1", Order = "2", Section = "1.2", Title = "one two" }); sec.Add(new section { Group = "2", Order = "1", Section = "2.1", Title = "two one" }); sec.Add(new section { Group = "2", Order = "2", Section = "2.2", Title = "two two" }); sec.Add(new section { Group = "3", Order = "1", Section = "3.1", Title = "three one" }); //creates a diction with all the sections in order grouped by group. var jmp = sec.OrderBy(j => j.Order).GroupBy(c => c.Group).ToDictionary(c => c.Key, d => d.ToList()); //gets one section form a section var wantedSection = sec.FirstOrDefault(c => c.Section == "2.2"); //return null if no iteem found //gets one section in the order you set in order var sectionOne = sec.Where(c => c.Group == "1").OrderBy(c => c.Order).ToList(); }
Showing posts with label least effort. Show all posts
Showing posts with label least effort. Show all posts
Wednesday, May 18, 2011
Some Fun With Linq.
A friend of mine was asking me how I search through dictionary that contain lists of objects with Linq. After I chatted with him for a bit. I realized I don't do that. I have one huge list that I parse out with linq. It makes my life easier. Here is a code example of what I would use.
Wednesday, May 6, 2009
Some times you just have to rename files.
So I needed to rename a bunch of files. I needed to add the size of the file as a postfix. Same name just an adition of 16x16 which when doing it by hand is tedious. So this little script was created. You have to dump it into the same folder as the files you are working with, and you have to put the path in walkPath. I could have it pull the current directory from the os but that would take work.
import os
walkPath = r"some disk path"
for d in os.walk(walkPath):
dirpath, dirnames, filenames = d
for a in filenames:
if a.endswith(".py"):
pass
else:
print a
aSplit = a.split(".")
os.rename(a,aSplit[0]+"16x16."+aSplit[1])
Subscribe to:
Posts (Atom)