开发者

How to send the Anonymous type object to a method

I am using Linq using Entity Framework to query MySQL Database as 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?开发者_运维问答


The only typed way you can do that is if the other method is generic, and you let generic type inference do the work:

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.


You probably should't do that. Without the type information your method won't (easily) be able to access the properties of the object.

Use a concrete user defined type instead. If your object is very short-lived and you don't want to create a new type you could use a Tuple (requires .NET 4 or newer).


You could also use the C# dynamic keyword. Of course it is a slow as reflection and type unsafe. For example:

void SomeMethod(dynamic d)
{
    Console.WriteLine(d.Client);
    Console.WriteLine(d.GlobalList.Count);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜