An example of what you can do with LINQ(code snippets):
SortedDictionary<int, Customer> customers = new SortedDictionary<int,Customer> { {1,new Customer{Name="John",Age=13,Gender="M"}}, {2,new Customer{Name="Mary",Age=25,Gender="F"}}, {3,new Customer{Name="Jennifer",Age=27,Gender="F"}}, {4,new Customer{Name="Johnathan",Age=35,Gender="M"}}, {5,new Customer{Name="Catrine",Age=23,Gender="F"}}, {6,new Customer{Name="Henry",Age=30,Gender="M"}}, {7,new Customer{Name="Heny",Age=29,Gender="F"}} }; var q = from s in customers where s.Value.Age < 25 && s.Value.Gender=="F" select s; foreach(var t in q) { Console.WriteLine(t.Value.Name); // "Catrine" } Console.ReadLine();
//Customer class
class Customer { public string Name { get; set; } public int Age { get; set; } public string Gender { get; set; } }
The above example basically use LINQ to filter out items in SortedDictionary based on age (<25) and gender("female").
I hope this forum will kick-start LINQ discussions..
hannes:Here is a nice use of Union which cuts out a bit of code.The scenario is that default values need to be added to a list where no value has been defined (in this example for each FeeID).IEnumerable<Fee> feeOverrides; // set to some listIEnumerable<Fee> feeDefaults; // set to some listIEnumerable<Fee> fees = feeOverrides.Union(feeDefaults, new FeeComparer());However, I could not figure out how to use a lambda expression or some other shorthand to do the comparison on objects.. so I added this class. Maybe someone could help. internal class FeeComparer : IEqualityComparer<Fee> { public bool Equals(Fee x, Fee y) { return x.FeeID.Equals(y.FeeID); } public int GetHashCode(Fee obj) { return 0; } }
For what i understand, Lambda expression can only be used if method accepts delegates as argument. In this case there is no delegates arguments for Union method, Therefore lambda expression cannot be used. Please correct me if I am wrong
Continuing on the Linq thread, its getting awfully quiet here :(
anyone knew about the PLINQ - (Parallel Linq) from the extensions CTP (free downloadable from ms)
Wondering if anyone here worked on it yet? It sounds cool and claims to utilises the multi-core processors that new machines have. I personally, havent try it yet, because the CTP requires 3.5 framework (beta version will not do)... havent upgrade to that on my box yet...
Anyway, my concern is that do we really need a dedicated API just to make use of threading and parallelism? Why cant the framework or perhaps the OS kernel itself, handle the need for threading itself without us developers specifying it to do so? Hmm, just my thought, perhaps after trying it out, my concern will be satisfied...
Anyway it changes the way we do parallel programming I suppose.....
FYI: Channel9.msdn have some nice video clip tutorials
http://devpinoy.org/blogs/cruizer
I dunno.. PLINQ seems a bit specialized. If you have long-running tasks maybe multi-threading is simpler and more predictable...
I'd hate to have to keep all the side-effects of using PLINQ in mind, or have to debug PLINQ. And what would a junior developer do with PLINQ??
Here's how we can do it in F#
let tasks = [ for n in 1 .. someLimit -> doLongRunningIntensiveProcess(n) ]
Async.Run (Async.Parallel tasks)
wherein the function doLongRunningIntensiveProcess is defined as
let doLongRunningIntensiveProcess(n) =
async{
... code ..
}