Technorati Tags: Песня о войне
Thursday, January 28, 2010
Thursday, January 21, 2010
LINQ extension method to Capitalize string
LINQ extension method to Capitalize first letter of each word in a sentence:
public static string Capitalize(this string s)
{
return s.ToCharArray().Aggregate(String.Empty,
(currentWord, nextWord) =>
currentWord.Length == 0 && nextWord != ' ' ? nextWord.ToString().ToUpper() : (
currentWord.EndsWith(" ") ? currentWord + nextWord.ToString().ToUpper() :
currentWord + nextWord.ToString()
)
);
}
Sunday, January 17, 2010
Thursday, January 14, 2010
Return collection of anonymous type from method in LINQ
Technorati Tags: LINQ
Let’s say you have some method that execute a LINQ query over collection and constructs a collection of (new) anonymous type as result. Here is how you can return it and use it below the scope of the method:public class Program { public static IEnumerable<T> GetExampleCollection<T>(T type) { // generate some random collection simulating source data var random = new Random(); var collection = Enumerable.Repeat(0, 100).Select( i => new { ClientId = random.Next(), OrderId = random.Next(), Price = random.NextDouble() }); // run some linq query over it var queryOverCollection = from item in collection where item.ClientId % 2 == 0 select new { DoublePrice = item.Price * 2.0 }; IQueryable v = (from item in collection where item.ClientId % 2 == 0 select new { DoublePrice = item.Price * 2.0 }).AsQueryable(); return queryOverCollection as IEnumerable<T>; } static void Main(string[] args) { // bring collection of anonymous type items from a method! var v = GetExampleCollection(new { DoublePrice = new double() }); foreach (var item in v) { // ... and use it! Console.WriteLine(item.DoublePrice); } } }
Note, that anonymous type is resolved on the assemble level, so you can’t reference it in another assembly.
Wednesday, January 13, 2010
SQL connection string application name
Add “Application Name=My Application Name” to connection string to differentiate your connections (e.g. in SQL Profiler).
(it’s a self reminder post).
(it’s a self reminder post).
Subscribe to:
Posts (Atom)