开发者

How to return Generic.List<Anonymoustype> from a function in C#

ASP.NET 3.5 C#

I am joining two tables using Linq.

Table names are MapAssets and ExitPoint.

In Database they are related with 'has a relationship'

I am writing a function in my BLL to return the joined table

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

This obviuosly doesn't work. And I dont know what to return at all.

All constraint I have for the return is to be able to be bound to a gridview.

is this the correct way? Or else whats the correct way?


You can't, or better, the only way is to return them boxed in a List of object, but this hugely complicates things, because you can't cast them to any type (of course it's anonymous) and you can only access their properties through reflection....

In cases like that, I'd highly suggest you to create a custom class.

EDIT:

On a side note...
If you were using .net 4, things would be easier because you could returns dynamic Type instead of object (look at this link to see dynamic's simplifications), but I'd prefer to create a custom class anyway.


Have a look at how to return anonymous types from Method.

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

Copying the code from the link.

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

You can use the method mentioned below to return List and

List<object> lstAnonymousTypes = GetExitPointDetailsByProjectID(1);

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

Hope this helps not tried.


You can't return an anonymous type, you can only use an anonymous type in the scope of the method it's in. You could need to create a new class with MapAssets/ExitPoints properties and select a new instance of that class.


You are trying to return List ExitPoints and List of MapAssets which is not possible because you are getting the output from both tables ie ExitPoints and MapAssets. And it is also not possible to return an anonymous type. So in order to retrun the query create a class name ExMapClass with properties that you need as output of the queries. Now after executing the linq query which you have written iterate it ie

create list of newly created class

list newclass = new list ();

foreach( var result in ctx ) {

instantiate the created class

obj.Property1 = var.MapAssets;

obj.Property2 = var.ExitPoints;

newclass.add(obj);

}

now retrun the list of newlycreated class.

hope you got it.


Do you have to bind to this object after you've created it? If not then you can create an "persistent AnonymousType" class that stores the values in a dictionary and returns the property values with a method like:

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

Here is a link to an excellent example. The author also has an example where he creates the "AnonymousType" from a datatable.

I have worked on a variation of this where I provide the ability to query a list of "AnonymousType" with the following syntax:

// Here's the query var dept13 = anonAgents.AsQueryable() .Where(x => x.Has("Department", Compare.Equal, 13);

// Here is how the List is constructed

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


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


wont this work ?

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜