Anonymous Types, LINQ and Generics

  • Thread starter Stefan Hoffmann
  • Start date
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();
}
}
}
 
M

miher

Hi,

I think You don't have to explicitly specify the type, the compiler is
clever enought to infer it.

WriteLine(complexVar, w => String.Format("{0} {1} {2}.", w.Count, w.Key.Tab,
w.Key.Value), "complex, untyped LINQ");

Hope You find this useful.
-Zsolt
 
S

Stefan Hoffmann

hi Zsolt,
I think You don't have to explicitly specify the type, the compiler is
clever enought to infer it.
Ouch, didn't thought of this.
WriteLine(complexVar, w => String.Format("{0} {1} {2}.", w.Count,
w.Key.Tab, w.Key.Value), "complex, untyped LINQ");
So simple :) Thanks.


mfG
--> stefan <--
 
P

Pavel Minaev

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?

No, anonymous types are called anonymous precisely because they do not
have names :) As such, you cannot directly reference them (though you
can capture an anonymous type as a type parameter - this is in fact
precisely what you do when you rely on type inference, as suggested
earlier in this thread).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top