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 the database, they are related to "related"

I am writing a function in my BLL to return a 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 does not work. And I do not know what to return at all.
All restrictions that I have to return must be bound to a gridview.
Is this the right way? Or how right?

0
source share
7 answers

You cannot, or better, the only way to return them in a box to a List from object , but this makes the situation very difficult, because you cannot attribute them to any type (of course, this is anonymous), and you can access their properties through reflection ....

In such cases, I highly recommend that you create a custom class.

EDIT:

On a side note ...
If you use .net 4, everything will be simpler because you can return a dynamic Type instead of an object (look at this link to see the simplifications of dynamic ), but I would rather create a custom class.

+6
source

See how to return anonymous types from a method.

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

Copy code from 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 a 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 to try.

+2
source

You cannot return an anonymous type; you can only use an anonymous type in the scope of the method in which it resides. You may need to create a new class with MapAssets / ExitPoints properties and select a new instance of this class.

+1
source

You are trying to return List ExitPoints and MapAssets, which is not possible because you are getting output from both tables, i.e. ExitPoints and MapAssets. And it is also impossible to return an anonymous type. Thus, to receive a request, create an ExMapClass class name with the properties that you need to output the requests. Now, after executing the linq query you wrote, iterates, i.e.

create a list of a newly created class

list newclass = new list ();

foreach (var result in ctx) {

create an instance of the created class

obj.Property1 = var.MapAssets;

obj.Property2 = var.ExitPoints;

newclass.add (OBJ);

}

now retrieves a list of the newly created class.

hope you received it.

+1
source

Do I need to bind to this object after its creation? If not, then you can create a persistent AnonymousType class that stores values ​​in the dictionary and returns property values ​​using a method, for example:

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

Here is a link to a great example . The author also has an example where he creates an "Anonymous Type" from a datatable.

I worked on changing this where I provide the query option in the Anonymous Type list with the following syntax:

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

// This is how List is created

 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; } 
+1
source

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

will not 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(); 
-2
source

Source: https://habr.com/ru/post/1333634/


All Articles