S
Stefan Hoffmann
hi @all,
I have an anonymous type from a LINQ query:
var complexVar = from b in list
group b by new { b.Tab, b.Value } into g
select new { Key = g.Key, Count = g.Count() };
Can I determine the type, so that I can use it in the following example
using Generics?
WriteLine<???>(complexVar,
w => String.Format("{0} {1} {2}.", w.Count, w.Key.Tab, w.Key.Value),
"complex, untyped LINQ");
mfG
--> stefan <--
--
namespace AnonymousGenerics
{
using System;
using System.Collections.Generic;
using System.Linq;
internal class Bucket
{
public string Tab { get; set; }
public string Value { get; set; }
public Bucket(string tab, string value)
{
this.Tab = tab;
this.Value = value;
}
}
internal class Program
{
private static void WriteLine<T>(IEnumerable<T> list, Func<T,
String> formatter, String caption)
{
String line = new String('-', caption.Length);
Console.WriteLine(caption + "\n" + line);
foreach (var item in list)
{
Console.WriteLine(formatter(item));
}
Console.WriteLine();
}
private static void Main(string[] args)
{
Bucket[] list = new Bucket[]
{
new Bucket("A1", "c1"), new Bucket("A1", "c1"),
new Bucket("A1", "c1"), new Bucket("A1", "c1"),
new Bucket("A1", "c1"), new Bucket("A3", "23"),
new Bucket("A3", "23"), new Bucket("A3", "23"),
new Bucket("A6", "45"), new Bucket("A6", "45"),
new Bucket("A1", "7"), new Bucket("A1", "7"),
new Bucket("A1", "7")
};
var simpleVar = from v in list
group v by new { v.Tab, v.Value } into g
select g.Count();
WriteLine<int>(simpleVar, w => w.ToString(), "simple,
untyped LINQ");
IEnumerable<int> simpleInt = list
.GroupBy(i => new { Tab = i.Tab, Value = i.Value })
.Select(i => i.Count());
WriteLine<int>(simpleInt, w => w.ToString(), "simple, typed
extension method");
var complexVar = from b in list
group b by new { b.Tab, b.Value } into g
select new { Key = g.Key, Count = g.Count() };
foreach (var r in complexVar)
{
Console.WriteLine(String.Format("{0} {1} {2}", r.Count,
r.Key.Tab, r.Key.Value));
}
//WriteLine<???>(complexVar, w => String.Format("{0} {1}
{2}.", w.Count, w.Key.Tab, w.Key.Value), "complex, untyped LINQ");
Console.ReadLine();
}
}
}
I have an anonymous type from a LINQ query:
var complexVar = from b in list
group b by new { b.Tab, b.Value } into g
select new { Key = g.Key, Count = g.Count() };
Can I determine the type, so that I can use it in the following example
using Generics?
WriteLine<???>(complexVar,
w => String.Format("{0} {1} {2}.", w.Count, w.Key.Tab, w.Key.Value),
"complex, untyped LINQ");
mfG
--> stefan <--
--
namespace AnonymousGenerics
{
using System;
using System.Collections.Generic;
using System.Linq;
internal class Bucket
{
public string Tab { get; set; }
public string Value { get; set; }
public Bucket(string tab, string value)
{
this.Tab = tab;
this.Value = value;
}
}
internal class Program
{
private static void WriteLine<T>(IEnumerable<T> list, Func<T,
String> formatter, String caption)
{
String line = new String('-', caption.Length);
Console.WriteLine(caption + "\n" + line);
foreach (var item in list)
{
Console.WriteLine(formatter(item));
}
Console.WriteLine();
}
private static void Main(string[] args)
{
Bucket[] list = new Bucket[]
{
new Bucket("A1", "c1"), new Bucket("A1", "c1"),
new Bucket("A1", "c1"), new Bucket("A1", "c1"),
new Bucket("A1", "c1"), new Bucket("A3", "23"),
new Bucket("A3", "23"), new Bucket("A3", "23"),
new Bucket("A6", "45"), new Bucket("A6", "45"),
new Bucket("A1", "7"), new Bucket("A1", "7"),
new Bucket("A1", "7")
};
var simpleVar = from v in list
group v by new { v.Tab, v.Value } into g
select g.Count();
WriteLine<int>(simpleVar, w => w.ToString(), "simple,
untyped LINQ");
IEnumerable<int> simpleInt = list
.GroupBy(i => new { Tab = i.Tab, Value = i.Value })
.Select(i => i.Count());
WriteLine<int>(simpleInt, w => w.ToString(), "simple, typed
extension method");
var complexVar = from b in list
group b by new { b.Tab, b.Value } into g
select new { Key = g.Key, Count = g.Count() };
foreach (var r in complexVar)
{
Console.WriteLine(String.Format("{0} {1} {2}", r.Count,
r.Key.Tab, r.Key.Value));
}
//WriteLine<???>(complexVar, w => String.Format("{0} {1}
{2}.", w.Count, w.Key.Tab, w.Key.Value), "complex, untyped LINQ");
Console.ReadLine();
}
}
}