How to send an anonymous type object to a method

I am using Linq, using the Entity Framework to query a MySQL database, as shown below -

var query = from c in subQuery select new { Client = c.Client, GlobalList = c.GlobalList, Book = (from book in context.Books where book.c_clt_id == c.Client.c_clt_id select book) }; var totalSearch = query.ToList(); 

Now I want to pass totalSearch as a parameter to another method. Please help me how can this be done?

+4
source share
3 answers

You can also use the C # dynamic keyword. Of course, this is a slow reflection and the type is unsafe. For instance:

 void SomeMethod(dynamic d) { Console.WriteLine(d.Client); Console.WriteLine(d.GlobalList.Count); } 
+1
source

The only typed way you can do this is if the other method is common, and you let you type in the type of the general type:

 void SomeOtherMethod<T>(List<T> list) {...} ... SomeOtherMethod(totalSearch); 

You can also pass it without any type information via IList , IEnumerable , object or dynamic , of course.

+3
source

You probably shouldn't do that. Without type information, your method cannot (easily) access the properties of the object.

Use a specific user type instead. If your object is very short-lived and you do not want to create a new type, you can use Tuple (requires .NET 4 or later).

+2
source

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


All Articles