Return anonymous type results?

后端 未结 16 1599
梦毁少年i
梦毁少年i 2020-11-22 03:01

Using the simple example below, what is the best way to return results from multiple tables using Linq to SQL?

Say I have two tables:

Dogs:   Name, A         


        
16条回答
  •  野的像风
    2020-11-22 04:02

    Just to add my two cents' worth :-) I recently learned a way of handling anonymous objects. It can only be used when targeting the .NET 4 framework and that only when adding a reference to System.Web.dll but then it's quite simple:

    ...
    using System.Web.Routing;
    ...
    
    class Program
    {
        static void Main(string[] args)
        {
    
            object anonymous = CallMethodThatReturnsObjectOfAnonymousType();
            //WHAT DO I DO WITH THIS?
            //I know! I'll use a RouteValueDictionary from System.Web.dll
            RouteValueDictionary rvd = new RouteValueDictionary(anonymous);
            Console.WriteLine("Hello, my name is {0} and I am a {1}", rvd["Name"], rvd["Occupation"]);
        }
    
        private static object CallMethodThatReturnsObjectOfAnonymousType()
        {
            return new { Id = 1, Name = "Peter Perhac", Occupation = "Software Developer" };
        }
    }
    

    In order to be able to add a reference to System.Web.dll you'll have to follow rushonerok's advice : Make sure your [project's] target framework is ".NET Framework 4" not ".NET Framework 4 Client Profile".

提交回复
热议问题