简体   繁体   English

从函数返回匿名类型

[英]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"));

}

No you can't return an anonymous type from the method. 不,您无法从该方法返回匿名类型。 For more info read this MSDN doc. 有关详细信息,请阅读 MSDN文档。 Use class or struct instead of an anonymous type. 使用classstruct而不是anonymous类型。

You should read blog post - Horrible grotty hack: returning an anonymous type instance 你应该阅读博客文章 - 可怕的grotty hack:返回一个匿名类型的实例

If you are using framework 4.0 then you can return List<dynamic> but be careful to access the properties of anonymous object. 如果您使用的是框架4.0,则可以返回List<dynamic>但要小心访问匿名对象的属性。

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>();
}

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. 使用示例实例创建一个帮助方法以强制转换为匿名类型

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;
}

Just use and ArrayList 只需使用和ArrayList

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

              items.AddRange(yourVariable 
                        .Where(p => p.yourproperty == something)
                        .ToList());
            return items;
    }

Use object , not var . 使用object ,而不是var You'll have to use reflection to access the properties outside the scope of the anonymous type though. 但是,您必须使用反射来访问匿名类型范围之外的属性。

ie

private object GetRowGroups(string columnName) 
...
var RowGroupList = new List<object>();
...

From C#7 you can return a list of tuples: 从C#7开始,您可以返回元组列表:

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

You can even give the members of the tuple names: 你甚至可以给元组名称的成员:

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

However you need System.ValueTuple from the Nuget Package Manager. 但是,您需要Nuget包管理器中的System.ValueTuple

Anyway I suggest to give things a clear name that includes what their purpose is, in particular when this is part of a public API. 无论如何,我建议给出明确的名称,包括它们的用途,特别是当它是公共API的一部分时。 Having said this you should consider to create a class that holds those properties and return a list of that type. 说完这些之后,您应该考虑创建一个包含这些属性的类并返回该类型的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM