Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Anonymous Type from a function

Can I use an anonymous type as a return type in a Function, and then stuff that returned value into an array or collection of some sort whilst also adding an additional field to the new array/collection? excuse my pseudocode...

private var GetRowGroups(string columnName)
{
var groupQuery = from table in _dataSetDataTable.AsEnumerable()
                             group table by new { column1 = table[columnName] }
                                 into groupedTable
                                 select new
                                 {
                                     groupName = groupedTable.Key.column1,
                                     rowSpan = groupedTable.Count()
                                 };
    return groupQuery;

}

private void CreateListofRowGroups()
{
    var RowGroupList = new List<????>();
    RowGroupList.Add(GetRowGroups("col1"));
    RowGroupList.Add(GetRowGroups("col2"));
    RowGroupList.Add(GetRowGroups("col3"));

}
like image 222
Dkong Avatar asked Sep 21 '11 02:09

Dkong


People also ask

How do I return anonymous type?

As in the preceding facts you cannot return an anonymous type from a method, if you do want to return one then you need to cast it to an object.

How do I return an anonymous function in C#?

string temp = ((Func<string>)(() => { return "test"; }))(); string temp = new Func<string>(() => { return "test"; })(); Note: Both samples could be shorted to the expression form which lacks the { return ... } Thanks.

How do you define anonymous type?

Essentially an anonymous type is a reference type and can be defined using the var keyword. You can have one or more properties in an anonymous type but all of them are read-only. In contrast to a C# class, an anonymous type cannot have a field or a method — it can only have properties.


4 Answers

This is a very popular question. In general you cannot return an anonymous type due to the requirement of strong typing. However there are a couple of workarounds.

  1. Create a simple type to represent the return value. (See here and here). Make it simple by generating from usage.
  2. Create a helper method to cast to the anonymous type using a sample instance for casting.
like image 79
mellamokb Avatar answered Oct 15 '22 18:10

mellamokb


No you can't return an anonymous type from the method. For more info read this MSDN doc. Use class or struct instead of an anonymous type.

You should read blog post - Horrible grotty hack: returning an anonymous type instance

If you are using framework 4.0 then you can return List<dynamic> but be careful to access the properties of anonymous object.

private List<dynamic> GetRowGroups(string columnName) { var groupQuery = from table in _dataSetDataTable.AsEnumerable()                              group table by new { column1 = table[columnName] }                                  into groupedTable                                  select new                                  {                                      groupName = groupedTable.Key.column1,                                      rowSpan = groupedTable.Count()                                  };     return groupQuery.ToList<dynamic>(); } 
like image 25
KV Prajapati Avatar answered Oct 15 '22 16:10

KV Prajapati


No, you can't return an anonymous type directly, but you can return it using an impromptu interface. Something like this:

public interface IMyInterface
{
    string GroupName { get;  }
    int RowSpan { get; }
}

private IEnumerable<IMyInterface> GetRowGroups()
{
    var list =
        from item in table
        select new
        {
            GroupName = groupedTable.Key.column1,
            RowSpan = groupedTable.Count()
        }
        .ActLike<IMyInterface>();

    return list;
}
like image 31
Chris Fulstow Avatar answered Oct 15 '22 16:10

Chris Fulstow


Just use and ArrayList

    public static ArrayList GetMembersItems(string ProjectGuid)
    {
        ArrayList items = new ArrayList(); 

              items.AddRange(yourVariable 
                        .Where(p => p.yourproperty == something)
                        .ToList());
            return items;
    }
like image 34
Alex Z Avatar answered Oct 15 '22 16:10

Alex Z