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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:14:00+00:00 2026-05-26T06:14:00+00:00

I need to find the biggest size of each string column in a database

  • 0

I need to find the biggest size of each string column in a database as one of the informations to design another database. The only access I have to the source database is by a web service. I can just do it for each of the many columns to find the biggest size but I want it generic so I can use it later.

I wrote this very simplified version to make it simple to understand. Two of the last lines have invented syntax and it is where I need help.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class myClass
    {
        private string s;
        public string S
        {
            get { return s; }
            set { s = value; }
        }
        private int i;
        public int I
        {
            get { return i; }
            set { i = value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Type myClassType = typeof(myClass);
            System.Reflection.PropertyInfo[] propertyInfo = myClassType.GetProperties();
            Dictionary<string, int> property = new Dictionary<string, int>();

            foreach (System.Reflection.PropertyInfo info in propertyInfo)
               if (info.PropertyType == typeof(System.String))
                    property.Add(info.Name, -1);

            myClass[] myPa = new myClass[2];
            myPa[0] = new myClass();
            myPa[0].S = "1";
            myPa[0].I = 0;
            myPa[1] = new myClass();
            myPa[1].S = "12";
            myPa[1].I = 1;

This is where I need help. I invented the c[pair.key]. How to have a reference to a property that I know the name of?

            foreach (myClass c in myPa)
                foreach (KeyValuePair<string, int> pair in property)
                    if (c[pair.Key].Length > pair.Value)
                        property[pair.Key] = c[pair.Key].Length;

            foreach (KeyValuePair<string, int> pair in property)
                Console.WriteLine("Property: {0}, Biggest Size: {1}", pair.Key, pair.Value);
        }
    }
}

Output shoud be:

Property: S Biggest Size: 2
  • 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-26T06:14:01+00:00Added an answer on May 26, 2026 at 6:14 am

    The following should suffice:

    static void Main()
    {
        // Demo data
        myClass[] myPa = new myClass[2];
        myPa[0] = new myClass();
        myPa[0].S = "1";
        myPa[0].I = 0;
        myPa[1] = new myClass();
        myPa[1].S = "12";
        myPa[1].I = 1;
    
        PrintMaxLengthsOfStringProperties(myPa);
    }
    
    public static void PrintMaxLengthsOfStringProperties<T>(IEnumerable<T> items)
    {
        var t = typeof(T);
        t.GetProperties().Where(p => p.PropertyType == typeof(string)) // TODO: Add other filters
                            .SelectMany(p => items.Select(o => (string)p.GetValue(o, null)).Select(v => new { Property = p, Value = v }))
                            .GroupBy(u => u.Property)
                            .Select(gu => new { Property = gu.Key, MaxLength = gu.Max(u => u.Value != null ? u.Value.Length : 0) })
                            .ToList()
                            .ForEach(u2 => Console.WriteLine("Property: {0}, Biggest Size: {1}", u2.Property.Name, u2.MaxLength))
                            ;
    }
    

    Though you might add some extra filters on the “GetProperties” result set (such as taking out static ones, or indexing properties, etc.

    It makes use of a couple of Linq extension functions, namely, “Where”, “GroupBy”, “SelectMany”, “Select”, and “Max” as well using anonymous types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find a way to crawl one of our company's web applications
My gmail is getting full... I need a method to find out the biggest
I have a string. In this string i need replace all special characters (0-31
I have N keys. I need to find a data structure which i can
This is an Oracle question. I need to find the top 5 biggest values
I have the html page, and me need find some elements with tag a
I need to find out how to format numbers as strings. My code is
I need to find a bottleneck and need to accurately as possible measure time.
I need to find the PID of the current running process on a Linux
I need to find/create an application that will create employee web usage reports from

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.