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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:52:23+00:00 2026-06-09T06:52:23+00:00

I have an enumerable as follows: IEnumerable<dynamic> collection = new { column1 = 1,

  • 0

I have an enumerable as follows:

 IEnumerable<dynamic> collection = new 
 {  
    column1 = "1", column2 = "name", column3= "somevalue" 
 }, 
 new 
 { 
    column1 = "2",   column2 = "name2", column3= "somevalue2" 
 },
 new 
 { 
    column1 = "3", column2 = "name3", column3= "somevalue3" 
 }

and so on….The number of columns in the dynamic types can also vary.No of columns can be more or less.

Now if I want to search this IEnumerable collection for e.g. somevalue2, I need the following information
Found in Row 2 (Assume column1 is always primary key) in Column “column2”.
Which would be the best way to translate this dynamic collection into a data structure (.NET List/Collection/Hashtable etc.) and the best way to query the information from the collection. The collection will at most will be a page of data (25 rows).
Any code snippets and data structures to do a super fast efficient way of searching the data would be appreciated. Assume the search term is “contains” match as opposed to a perfect match.

  • 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-09T06:52:26+00:00Added an answer on June 9, 2026 at 6:52 am

    Those are anonymous types; anonymous types are a compiler feature, so that layout is fixed rigid at compile time, so they aren’t going to vary. I would suggest: don’t use dynamic. For example:

     var arr = new[] {
       new { column1 = "1", column2 = "name", column3= "somevalue" }, 
       new { column1 = "2", column2 = "name2", column3= "somevalue2" },
       new { column1 = "3", column2 = "name3", column3= "somevalue3" }
     };
    

    which is all strongly typed and well-defined. You can use regular operations, for example:

    var item2 = arr.FirstOrDefault(x => x.column1 == "2");
    

    again, all static-typed.

    If you want to search all the columns, then I would suggest something like a dictionary is better:

     var arr = new[] {
       new Dictionary<string,string> { {"column1", "1"}, ... }, 
       new Dictionary<string,string> { {"column1", "2"}, ... }, 
       new Dictionary<string,string> { {"column1", "3"}, ... }, 
     };
    

    Since you can then just look at .Values.Contains(...)


    Edit: with the comment I think I understand it more clearly; I am picturing an opaque method that returns IEnumerable<dynamic> that are actually POCO types (not dynamic types) and we want to check the string members (arbitrary names) for a match. We should be able to do that with reflection, but FastMember will make it much faster:

    static void Main()
    {
        string search = "ame2";
    
        int rowIndex = 0;
        string[] names = null;
        TypeAccessor accessor = null;
        foreach(object row in GetData())
        {
            if(names == null)
            { // first row; get the property-names and build an accessor
                names = (from prop in row.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                         where prop.PropertyType == typeof (string)
                         select prop.Name).ToArray();
                accessor = TypeAccessor.Create(row.GetType());
            }
    
            foreach(var name in names)
            {
                var val = accessor[row, name] as string;
                if(val != null && val.Contains(search))
                {
                    Console.WriteLine("row {0}, col {1}, val {2}", rowIndex, name, val);
                }
            }
            rowIndex++;
        }
    }
    static IEnumerable<dynamic> GetData()
    {
        yield return new {column1 = "1", column2 = "name", column3 = "somevalue"};
        yield return new {column1 = "2", column2 = "name2", column3 = "somevalue2"};
        yield return new {column1 = "3", column2 = "name3", column3 = "somevalue3"};
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got an IEnumerable collection as follows var result1 = GetResult1() // Returns
i am new to wpf, i have datagrid as follows, <DataGrid Grid.Row=0 x:Name=dg1 Grid.Column=0
I have this following IEnumerable LINQ query: var query = from p in Enumerable.Range(2,
I need to average 3 IEnumerable collection and create a new IEnumerable with the
I have the follow method that returns a dynamic object representing an IEnumerable<'a> ('a=anonymous
If I have a sequence as follows (let's say it's an IEnumerable<T> ): [A,
I have this class that exposes an IEnumerable<Record> as follows (implementation details left out):
I have the code as follows: PropertyInfo p_info_keys = obj.GetType().GetProperty(Keys); IEnumerable<string> keys = (IEnumerable<string>)p_info_keys.GetValue(obj,
I have a pieces of code: The method is as follows: public IEnumerable<c> Test(){
I have an entity structure as follows: IManager: IDeletable { IEnumerable<IFund> Funds {get;} IFailureNotification

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.