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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:30:37+00:00 2026-05-30T05:30:37+00:00

I have a array of string say: String[] Fields=new String[]{RowField,RowField1} In which I can

  • 0

I have a array of string say:

String[] Fields=new String[]{RowField,RowField1}

In which I can use the below query to get the values by specifying the values is query i.e RowField and RowField1:

var Result = (
    from x in _dataTable.AsEnumerable()
    select new
    {
        Name = x.Field<object>(RowField), 
        Name1 = x.Field<object>(RowField1)
    })
    .Distinct();

But if suppose I have many values in the Array like:

String[] Fields= new String[]
{
    RowField,
    RowField1,
    RowField2,
    .......
    RowField1000
};
  • How can I use the query here without specifying each of the rowfield in the query?
  • How can i iterate through the array items inside the LINQ?
  • 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-30T05:30:38+00:00Added an answer on May 30, 2026 at 5:30 am

    Essentially, you want to retrieve specific fields from a DataTable without hardcoding the field names.

    The following code will return a single dictionary object per row with the fields you specify in your array. There is no need to create additional extension methods or comparers:

    var result = (from row in _dataTable.AsEnumerable()
                     let projection = from fieldName in fields
                          select new {Name = fieldName, Value = row[fieldName]}
                     select projection.ToDictionary(p=>p.Name,p=>p.Value));            
    

    The inner select picks the field values you need from each table row and stores them in the projection variable. The outer select converts this variable in a Dictionary

    You can iterate over the result to get specific fields like this:

    foreach (var row in result)
    {
        Console.WriteLine(row["field1"]);
    }
    

    EDIT:
    The above code doesn’t return distinct values. It is possible to return distinct values without writing a special comparer using group by but the code is not very pretty:

    var result = (from row in table.AsEnumerable()
                    let projection = from fieldName in fields
                                    select new { Name = fieldName, Value = row[fieldName] }
                    group projection by projection.Aggregate((v, p) =>
                        new {
                            Name = v.Name + p.Name, 
                            Value = (object)String.Format("{0}{1}", v.Value, p.Value)
                        }) into g
                    select g.FirstOrDefault().ToDictionary(p=>p.Name,p=>p.Value));  
    

    The Aggregate creates a new projection whose Name and Value properties are the concatenation of all name and value fields. The result of the aggregate is used to group all rows and return the first row of each group. It works but it is definitely ugly.

    It would be better to create a simple DictionaryComparer like the following code:

        public class DictionaryComparer<TKey,TValue>: EqualityComparer<Dictionary<TKey,TValue>>
        {
            public override bool Equals(Dictionary<TKey, TValue> x, Dictionary<TKey, TValue> y)
            {
                //True if both sequences of KeyValuePair items are equal
                var sequenceEqual = x.SequenceEqual(y);
                return sequenceEqual;
            }
    
            public override int GetHashCode(Dictionary<TKey, TValue> obj)
            {
                //Quickly detect differences in size, defer to Equals for dictionaries 
                //with matching sizes
                return obj.Count;
            }
        }
    

    This allows you to write:

            var result = (from row in table.AsEnumerable()
                         let projection = from fieldName in fields
                                          select new {Name = fieldName, Value = row[fieldName]}                                                                                    
                         select projection.ToDictionary(p=>p.Name,p=>p.Value))
                         .Distinct(new DictionaryComparer<string, object>());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an array of values: string[] text = new string[] { val1,
I have a string say string s =C:\\Data , I have an array which
Let's say I have an array of strings: string[] myStrings = new string[] {
Lets say I have an array like this: string [] Filelist = ... I
Say I have an array of strings: string[] strArray = {aa, bb, xx, cc,
Let's say I have these two arrays: string[] arr1 = new string[2]{Hello, Stack} string[]
I have this array: string[,] productData = new string[5,7]; I bind it to a
I have a string array: string[] authors = new string[3]; authors[0] = Charles Dickens;
Let's say I have this Hello.scala. object HelloWorld { def main(args: Array[String]) { println(Hello,
Say I have an array like so in my code behind: public static string[]

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.