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 8036363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:35:44+00:00 2026-06-05T02:35:44+00:00

public static MapFrom Map(this IDataReader idr, params string[] columns) { return new MapFrom {

  • 0
public static MapFrom Map(this IDataReader idr, params string[] columns)
{
     return new MapFrom { Columns = columns };
}
public static IEnumerable<TEntity> To<TEntity>(this MapFrom from, Func<TEntity, object> map)
{
     // logic here.
}    

public IEnumerable<FooEntity> Execute()
{
    using (DbCommand cmd = db.GetStoredProcCommand("GetListOfFoo"))
    {
        using (IDataReader idr = db.ExecuteReader(cmd))
        {
            return idr.Map("FooID", "FooOfPooAmount", "AnotherFooColumn", "BarID")
              .To<FooEntity>(x => new object[]{
                          x.FooID, x.FooOfPooAmount, x.AnotherFoo, x.BarID
            });
         }
     }
}

I wish to take a data reader and create a simple mapper that is easy to use and is also performant. I haven’t used expression trees much, but I feel like they may be needed. What I want to do is inspect the relationship of x to it’s array (The lambda expression), and then use this relationship to automatically map values appropriately and create a new List of Foo Entitiies.

MapFrom is just a container to carry information and allow fluent extension methods.

The point of these acrobatics is to have a simple way to specify relationships. I realize I could easily do something like in a different form, but I wanted to be able to list columns “fluently” [1] (as in typing column name as a string followed by comma then next column name) and then list the object properties in sequential order and infer the mapping from this. It it possible to implement the To method above? Are Expression Trees appropriate here? I figure I can easily enough infer type casting for the data reader fields from the FooEntity property types.

[1] In this context I do not mean fluent as in “Fluent API”

  • 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-06-05T02:35:46+00:00Added an answer on June 5, 2026 at 2:35 am

    I wrote here your MapFrom class, than you have to write the code to copy the value from columns to the property of an instance of FooEntity and use the yield return statement to return a collection of your entity, so add another method to the MapFrom class:

    public class MapFrom
    {
        private readonly IDictionary<string, string> columnMapDictionary =
            new Dictionary<string, string>();
    
        public MapFrom(string[] columns)
        {
            foreach (var column in columns)
            {
                columnMapDictionary.Add(column, null);
            }
        }
    
        public void To<T>(params Expression<Func<T, object>>[] properties)
        {
            var index = 0;
            foreach (var columnMap in columnMapDictionary.ToDictionary(k => k.Key))
            {
                var member = (properties[index++].Body as MemberExpression);
                var propertyName = member.Member.Name;
                columnMapDictionary[columnMap.Key] = propertyName;  
            }
        }
    }
    

    Use it as:

    var mapper = new MapFrom("Name", "Surname");
    mapper.To<FooEntity>(p => p.FirstName, p => p.LastName);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static Node createNodeFromXMLString(String xml) throws SAXException, IOException { return builder.parse(new ByteArrayInputStream(xml.getBytes())) .getDocumentElement(); }
public static string MakeWebSafe(this string x) { const string RegexRemove = @(<\s*script[^>]*>)|(<\s*/\s*script[^>]*>); return Regex.Replace(x,
public static string ToTrimmedString(this DataRow row, string columnName) { return row[columnName].ToString().Trim(); } Compiles fine
public static IQueryable<Institution> WithFunds(this IQueryable<Institution> query, IEnumerable<Fund> allowedFunds) { return query. } I want
public static byte[] sendRequest(String url ,Object params ,boolean isHttps){ if(isHttps){ HttpsURLConnection urlConnection = (HttpsURLConnection)newURL(url).openConnection();
public static string RatingCalculator(int input) { if (input < 10) { return string.Empty; }
public static void main(String[] args) { staples[] stemp = new staples[8]; int j; try
public static void main(String[] args) { int [][]shatner = new int[1][1]; int []rat =
public static void main(String args[]) { Random r = new Random(); BufferedImage buf =
public static void DeleteThreads(int threadID) { StringBuilder sb = new StringBuilder(); sb.Append(DELETE FROM dbo.Threads);

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.