What is the return type of my linq query?

前端 未结 4 952
野的像风
野的像风 2020-12-29 10:41

I have two tables A & B. I can fire Linq queries & get required data for individual tables. As i know what each of the tables will return as shown in example. But,

4条回答
  •  渐次进展
    2020-12-29 10:59

    The value you generate is called Anonymous Type and you can return it unless you return object like:

    private object GetJoinAAndB()
    {
        var query = from a in objA
                    join b in objB
                    on a.ID equals b.AID
                    select new { a.ID, a.Name, b.Address };
        return query.ToList();
    }
    

    There are two good solutions:
    1. is to generate a class to match the output and generate it like Kobi solution
    2. if you are using .net 4 you can return a dynamic type like

    private dynamic GetJoinAAndB()
    {
        var query = from a in objA
                    join b in objB
                    on a.ID equals b.AID
                    select new { a.ID, a.Name, b.Address };
        return query.ToList();
    }
    

    then you can use it later. You can search the internet about the advantage of using dynamic keyword.

提交回复
热议问题