【问题标题】:How to return Generic.List<Anonymoustype> from a function in C#如何从 C# 中的函数返回 Generic.List<Anonymoustype>
【发布时间】:2010-12-10 11:55:07
【问题描述】:

ASP.NET 3.5 C#
我正在使用 Linq 加入两个表。
表名是 MapAssets 和 ExitPoint。
在数据库中,它们与“有关系”相关

我正在我的 BLL 中编写一个函数来返回连接的表

        public List<ExitPoints> GetExitPointDetailsByProjectID(int iProjectID)
        {
            ctx = new CoreDBDataContext();
            var exitPointDetails = from ma in ctx.MapAssets
                                   join ep in ctx.ExitPoints
                                   on ma.MapAssetID equals ep.MapAssetID
                                   where ma.ProjectID == iProjectID
                                   select new
                                   {
                                       //would like to have data from both tables here
                                       ctx.MapAssets,
                                       ctx.ExitPoints
                                   };
            return exitPointDetails.ToList();
        }

这显然行不通。而且我根本不知道要返回什么。
我对返回的所有限制是能够绑定到网格视图。
这是正确的方法吗?否则正确的方法是什么?

【问题讨论】:

    标签: c# asp.net linq linq-to-sql


    【解决方案1】:

    你不能,或者更好的是,唯一的方法是将它们装箱在 objectList 中,但这会使事情变得非常复杂,因为你不能将它们转换为任何类型(当然它是匿名的) 并且您只能通过反射访问它们的属性....

    在这种情况下,我强烈建议您创建一个自定义类。

    编辑:

    附带说明...
    如果您使用的是 .net 4,事情会更容易,因为您可以返回 dynamic Type 而不是 object(查看此 link 以查看 dynamic 的简化),但我更喜欢创建自定义还是上课吧。

    【讨论】:

    • 他用的是3.5,不能用动态的。
    • .net 3.5 中没有动态
    • 我认为您不应该删除对动态的提及。只需提及“如果您使用的是 .NET 4.0,您可以使用动态类型 link”之类的内容。
    【解决方案2】:

    看看如何从 Method 返回匿名类型。

    http://forums.asp.net/t/1387455.aspx.

    复制链接中的代码。

    object ReturnAnonymous()  
    {  
      return new { Name="Faisal", City="Chakwal" };  
    }  
    
    // Application entry-point  
    void Main()  
    {  
    
      object o = ReturnAnonymous();  
    
       // This call to 'Cast' method converts first parameter (object) to the  
       // same type as the type of second parameter - which is in this case   
       // anonymous type with 'Name' and 'City' properties  
       var typed = Cast(o, new { Name="", City="" });  
       Console.WriteLine("Name={0}, City={1}", typed.Name, typed.City);  
     }  
    
     // Cast method - thanks to type inference when calling methods it   
     // is possible to cast object to type without knowing the type name  
     T Cast<T>(object obj, T type)  
     {  
       return (T)obj;  
     }  
    

    你可以使用下面提到的方法返回List和

    List<object> lstAnonymousTypes = GetExitPointDetailsByProjectID(1);
    
    foreach(object o in lstAnonymousTypes)
    {
       //Change it accordingly
       var typed = Cast(o, new { new MapAssets() , new ExitPoints() }); 
    }
    

    希望这有助于不要尝试。

    【讨论】:

      【解决方案3】:

      您不能返回匿名类型,只能在其所在方法的范围内使用匿名类型。您可能需要创建一个具有 MapAssets/ExitPoints 属性的新类并选择该类的新实例。

      【讨论】:

        【解决方案4】:

        您正在尝试返回 List ExitPoints 和 MapAssets 列表,这是不可能的,因为您正在从两个表(即 ExitPoints 和 MapAssets)中获取输出。而且也不可能返回匿名类型。因此,为了重新运行查询,请创建一个类名 ExMapClass,其中包含您需要作为查询输出的属性。现在在执行您编写的 linq 查询后对其进行迭代,即

        创建新创建类的列表

        list newclass= 新列表 ();

        foreach( var 结果为 ctx ) {

        实例化创建的类

        obj.Property1 = var.MapAssets;

        obj.Property2 = var.ExitPoints;

        newclass.add(obj);

        }

        现在重新运行新创建的类列表。

        希望你明白了。

        【讨论】:

        • 感谢百万伴侣。我使用'select new MyCustomClass'而不是'select new'而不是通过结果集再次迭代
        【解决方案5】:

        创建后是否必须绑定到该对象?如果没有,那么您可以创建一个“持久 AnonymousType”类,将值存储在字典中并使用如下方法返回属性值:

        string lastName AnonType.GetValue<string>("LastName");
        int age AnonType.GetValue<int>("Age");
        

        这是excellent example 的链接。作者还有一个example where he creates the "AnonymousType" from a datatable.

        我已经处理了这个where I provide the ability to query 的一个变体,一个“AnonymousType”列表,语法如下:

        // 这里是查询 var dept13 = anonAgents.AsQueryable() .Where(x => x.Has("部门", Compare.Equal, 13);

        // List 是这样构造的

        private static AnonymousType ProvisionAgent(string name, int department)
                {
                    return AnonymousType.Create(new
                    {
                        Name = name,
                        Department = department
                    });
                }
        
                private List<AnonymousType> CreateAnonAgentList()
                {
                    var anonAgents = new List<AnonymousType>();
        
                    //  Dave and Cal are in Department 13
                    anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Dan Jacobs", 13, 44)));
                    anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Calvin Jones", 13, 60)));
        
                    //  Leasing = Dept 45
                    anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Stanley Schmidt", 45, 36)));
                    anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Jeff Piper", 45, 32)));
                    anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Stewart Blum", 45, 41)));
                    anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Stuart Green", 45, 38)));
        
                    //  HR = Dept 21
                    anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Brian Perth", 21, 25)));
                    anonAgents.Add(AnonymousType.Create(CreateAgentAnonType("Katherine McDonnel", 21, 23)));
        
                    return anonAgents;
                }
        

        【讨论】:

          【解决方案6】:

          只需使用和ArrayList

              public static ArrayList GetMembersItems(string ProjectGuid)
              {
                  ArrayList items = new ArrayList(); 
                  items.AddRange(yourVariable 
                                  .Where(p => p.yourProperty == something)
                                  .ToList());
                  return items;
              }
          

          【讨论】:

            【解决方案7】:

            这行不通吗?

            ctx = new CoreDBDataContext();
            var exitPointDetails = from ma in ctx.MapAssets
                                   join ep in ctx.ExitPoints
                                       on ma.MapAssetID equals ep.MapAssetID
                                   where ma.ProjectID == iProjectID
                                   select Tuple.Create(ma, ep);
            return exitPointDetails.ToList();
            

            【讨论】:

            • 请注意,我担心返回类型
            猜你喜欢
            • 1970-01-01
            • 2020-04-07
            • 1970-01-01
            • 2014-08-29
            • 2011-11-21
            • 2012-06-17
            • 2020-02-17
            • 2013-10-12
            相关资源
            最近更新 更多