2

Possible Duplicate:
Accessing C# Anonymous Type Objects
Working with C# Anonymous Types

I'm using linq to entities to get id and full name from a table with customers.

  public IQueryable RegresaClientesPorEmpresa(int id_emp)
    {
        var clientes = from c in context.clientes
                       where c.IDEmpresa == id_emp
                       select new
                       {
                           c.IDCliente,
                           NomComp = c.Nombres +" "+ c.ApellidoP +" "+ c.ApellidoM
                       };
        return clientes;
    }

The result is used as the datasource of a combobox, then when SelectionChangeCommitted is triggered in my combo, I want the selected item to be added to a listbox:

var clientes = operaciones.RegresaClientesPorEmpresa(2);
 combo_cliente.DataSource = clientes;
 combo_cliente.DisplayMember = "NomComp";
 combo_cliente.ValueMember = "IDCliente";

 listBox_grupo.DisplayMember = "NomComp";
 listBox_grupo.ValueMember = "IDCliente";

 private void combo_cliente_SelectionChangeCommitted(object sender, EventArgs e)
    {
        listBox_grupo.Items.Add(combo_cliente.SelectedItem); 
    }

Until here everything is fine. Finally I want to get all "IDCliente"'s values from all items added to my listbox, the problem is that I don't know how to do it because every item is an Anonymous data type. Can anyone help me?

Community
  • 1
  • 1
Jorge Zapata
  • 2,316
  • 1
  • 30
  • 57

3 Answers3

4

The scope of an anonymous type is limited to the method in which it is "declared" (well it's not really declared, but you see what I mean). If you want to use the result of your query in another method, create a named type to hold the results.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • The scope of the type is not limited to the method. It's however tough to declare something of that type outside the method since the name of the type is a) unknown and b) even if it was know it would be impossible to use it since the name is invalid in C# – Rune FS Sep 30 '11 at 19:14
  • @RuneFS, technically you're right, the type does exist outside the method... and there is actually a way to use it in another method if you know its members (see Bala R's answer), but it's a little hacky. Declaring a named type is certainly cleaner – Thomas Levesque Sep 30 '11 at 19:17
  • Yes definitely create a named type. – Rune FS Sep 30 '11 at 19:20
2

You shouldn't return anonymous types from your methods if you're expecting to access their properties. It'll be easier on you if you define a class instead, because that establishes the contract of your method.

Jacob
  • 77,566
  • 24
  • 149
  • 228
1

just create a class to avoid anonymous type.

class Foo
{
   public string NomComp {get ; set;}
   public string IDCliente {get ; set;}
}

and do

select new Foo
{
     ...
}

to save some hassle.

or You can define

T Cast<T>(object obj, T type)
{
   return (T)obj;
}

and then use

object anonymousObject = GetSelection();
var selection = Cast(anonymousObject , new { IDCliente="", NomComp ="" });

and then you should be able to do selection.NomComp to get the property value.

Bala R
  • 107,317
  • 23
  • 199
  • 210
  • @downvoter care to provide a reason? – Bala R Sep 30 '11 at 19:12
  • if and only if RegresaClientesPorEmpresa and the use of Cast is in the same compilation unit. Otherwise you simply have two types with the same number of properties with equal names. Wasn't my DV though – Rune FS Sep 30 '11 at 19:13
  • Would casting from one anonymous class to another really work? I don't understand how it would. – Jacob Sep 30 '11 at 19:13
  • @Jacob it's a hack that's not recommended but is a workaround. When you create anonymous types with same property names and type, the CLR reuses the first type definition. – Bala R Sep 30 '11 at 19:15
  • @Jacob if both parts are in the same compilation unit. It's the same type not two different types – Rune FS Sep 30 '11 at 19:15
  • 3
    The `CastByExample` technique should be discouraged. It will not work across assemblies and by the time you reach this point, you should go ahead and define the type. – Anthony Pegram Sep 30 '11 at 19:16
  • 1
    same property name, same type _and_ defined in the same order – Rune FS Sep 30 '11 at 19:16
  • 2
    This happens to work with the current version of the compiler (if everything is in the same assembly, and you have the same property names and types), but it may not work in future versions. It relies on undocumented, un-guaranteed behavior. Bad idea to rely on it. – Joe White Sep 30 '11 at 19:20
  • Thanks guys for givin such great ideas, I ended this by using dynamic type in C#4 (as suggested in merged post). What do you think about this decision? – Jorge Zapata Sep 30 '11 at 19:37