Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 555063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:46:35+00:00 2026-05-13T11:46:35+00:00

For a remoting scenario, the result would be very good to receive as an

  • 0

For a remoting scenario, the result would be very good to receive as an array or list of Tuple objects (among benefits being strong typing).

Example: dynamically convert SELECT Name, Age FROM Table => List<Tuple<string,int>>

Question: are there any samples out there that, given an arbitrary table of data (like SQL resultset or CSV file), with types of each column known only at runtime, to generate code that would dynamically create a strongly-typed List<Tuple<...>> object. Code should be dynamically generated, otherwise it would be extremely slow.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:46:35+00:00Added an answer on May 13, 2026 at 11:46 am

    Edit: I changed the code to use the Tuple constructor instead of Tuple.Create. It currently works only for up to 8 values, but to add the ‘Tuple stacking’ should be trivial.


    This is a little bit tricky and implementation is kind of dependent on the datasource.
    To give an impression, I created a solution using a list of anonymous types as a source.

    As Elion said, we need to dynamically create an expression tree to call it afterward. The basic technique we employ is called projection.

    We have to get, at runtime the type information and create a ConstructorInfor of the Tuple(…) constructor according to the properties count. This is dynamic (although needs to be the same per record) per each call.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    
    class Program
    {
        static void Main(string[] args)
        {
    
            var list = new[]
                           {
                               //new {Name = "ABC", Id = 1},
                               //new {Name = "Xyz", Id = 2}
                               new {Name = "ABC", Id = 1, Foo = 123.22},
                               new {Name = "Xyz", Id = 2, Foo = 444.11}
                           };
    
            var resultList = DynamicNewTyple(list);
    
            foreach (var item in resultList)
            {
                Console.WriteLine( item.ToString() );
            }
    
            Console.ReadLine();
    
        }
    
        static IQueryable DynamicNewTyple<T>(IEnumerable<T> list)
        {
            // This is basically: list.Select(x=> new Tuple<string, int, ...>(x.Name, x.Id, ...);
            Expression selector = GetTupleNewExpression<T>();
    
            var expressionType = selector.GetType();
            var funcType = expressionType.GetGenericArguments()[0]; // == Func< <>AnonType..., Tuple<String, int>>
            var funcTypegenericArguments = funcType.GetGenericArguments();
    
            var inputType = funcTypegenericArguments[0];  // == <>AnonType...
            var resultType = funcTypegenericArguments[1]; // == Tuple<String, int>
    
            var selects = typeof (Queryable).GetMethods()
                .AsQueryable()
                .Where(x => x.Name == "Select"
                );
    
            // This is hacky, we just hope the first method is correct, 
            // we should explicitly search the correct one
            var genSelectMi = selects.First(); 
            var selectMi = genSelectMi.MakeGenericMethod(new[] {inputType, resultType}); 
    
            var result = selectMi.Invoke(null, new object[] {list.AsQueryable(), selector});
            return (IQueryable) result;
    
        }
    
        static Expression GetTupleNewExpression<T>()
        {
            Type paramType = typeof (T);
            string tupleTyneName = typeof (Tuple).AssemblyQualifiedName;
            int propertiesCount = paramType.GetProperties().Length;
    
            if ( propertiesCount > 8 )
            {
                throw new ApplicationException(
                    "Currently only Tuples of up to 8 entries are alowed. You could change this code to allow stacking of Tuples!");
            }
    
            // So far we have the non generic Tuple type. 
            // Now we need to create select the correct geneeric of Tuple.
            // There might be a cleaner way ... you could get all types with the name 'Tuple' and 
            // select the one with the correct number of arguments ... that exercise is left to you!
            // We employ the way of getting the AssemblyQualifiedTypeName and add the genric information 
            tupleTyneName = tupleTyneName.Replace("Tuple,", "Tuple`" + propertiesCount + ",");
            var genericTupleType = Type.GetType(tupleTyneName);
    
            var argument = Expression.Parameter(paramType, "x");
    
            var parmList = new List<Expression>();
            List<Type> tupleTypes = new List<Type>();
    
            //we add all the properties to the tuples, this only will work for up to 8 properties (in C#4)
            // We probably should use our own implementation.
            // We could use a dictionary as well, but then we would need to rewrite this function 
            // more or less completly as we would need to call the 'Add' function of a dictionary.
            foreach (var param in paramType.GetProperties())
            {
                parmList.Add(Expression.Property(argument, param));
                tupleTypes.Add(param.PropertyType);
            }
    
            // Create a type of the discovered tuples
            var tupleType = genericTupleType.MakeGenericType(tupleTypes.ToArray());
    
            var tuplConstructor =
                tupleType.GetConstructors().First();
    
            var res =
                Expression.Lambda(
                    Expression.New(tuplConstructor, parmList.ToArray()),
                    argument);
    
            return res;
        }
    }
    

    If you want to use a DataReader or some CVS input, you would need to rewrite the function GetTupleNewExpression.

    I cant speak about the performance, although it should not be much slower as a native LINQ implementation as the generation of the LINQ expression only happens once per call.
    If its too slow you could go down the road of generating code (and keep it stored in a file) for example using Mono.Cecil.

    I couldn’t test this in C# 4.0 yet and but it should work.
    If you want to try it in C# 3.5 you need the following code as well:

    public static class Tuple
    {
    
        public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2)
        {
            return new Tuple<T1, T2>(item1, item2);
        }
    
        public static Tuple<T1, T2, T3> Create<T1, T2, T3>(T1 item1, T2 item2, T3 item3)
        {
            return new Tuple<T1, T2, T3>(item1, item2, item3);
        }
    }
    
    public class Tuple<T1, T2>
    {
    
        public Tuple(T1 item1, T2 item2)
        {
            Item1 = item1;
            Item2 = item2;
        }
    
        public T1 Item1 { get; set;}
        public T2 Item2 { get; set;}
    
        public override string ToString()
        {
            return string.Format("Item1: {0}, Item2: {1}", Item1, Item2);
        }
    
    }
    
    public class Tuple<T1, T2, T3> : Tuple<T1, T2>
    {
        public T3 Item3 { get; set; }
    
        public Tuple(T1 item1, T2 item2, T3 item3) : base(item1, item2)
        {
            Item3 = item3;
        }
    
        public override string ToString()
        {
            return string.Format(base.ToString() + ", Item3: {0}", Item3);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm struggling with getting IOC to work in a remoting scenario. I have my
Why would you use .NET Remoting over WCF ? I understand that WCF has
Very common scenario using Ajax is to parse the response and create the user
I’m trying to determine a good way to handle the scenario below. I have
Everyone is saying how .NET Remoting is being replaced by WCF, but I'm wondering
I would like to sync a single table with the result of a Join
I have a query about .Net Remoting, versioning and creating client activated objects. Here
Scenario. 3rd party admins want to administer systems with PS remoting/direct login of clients
I am using .Net Remoting and trying to access a remote object modified in
In .NET Remoting, Activator.GetObject method has a state parameter. What is the purpose of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.