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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:11:52+00:00 2026-05-10T23:11:52+00:00

Given a DataSet, I’m left joining DataTables[1-n] onto DataTable[0]. I have created a method

  • 0

Given a DataSet, I’m left joining DataTables[1-n] onto DataTable[0]. I have created a method with a signature as follows:

public DataTable LeftJoin(DataSet ds, params JoinKey[] JoinKey) 

Notes:

  • The return type is a DataTable, which is the resulting left join.
  • DataSet ds is a collection of DataTables for joining.
  • JoinKey is an object with two public properties: Type DataType and string Name. This collection contains the Type/Name of each field to use in the join.

Following are two snippets (Snippet 1 and Snippet 2) Snippet 1 works correctly. The only issue is that the second and third parameters of GroupJoin are hard-coded:

// hard-coded: br => new                                   {                                      zip = br.Field<string>('ZipCode'),                                      store =br.Field<double>'StoreID') },                                  jr => new                                  {                                      zip = jr.Field<string>('ZipCode'),                                      store = jr.Field<double>('StoreID')                                  } 

The above is not desirable. Instead, I would like to use my ‘JoinKey’ object to dynamically set the fields I want to join on (i.e., ZipCode and StoreID). I have attempted this in Snippet 2. First, however, please see Snippet 1.

Snippet 1 (working, hard-coded):

var dtBase = ds.Tables[0].AsEnumerable();   for (int i = 1; i < ds.Tables.Count; i++) {       var query = dtBase.GroupJoin(ds.Tables[i].AsEnumerable(),                                  br => new                                   {                                    zip = br.Field<string>('ZipCode'),                                    store = br.Field<double>('StoreID')                                  },                                  jr => new                                  {                                    zip = jr.Field<string>('ZipCode'),                                    store = jr.Field<double>('StoreID')                                  },                                  (baseRow, joinRow) => joinRow.DefaultIfEmpty()                                      .Select(row => new                                      {                                          flatRow = baseRow.ItemArray.Concat((row == null) ? new object[ds.Tables[i].Columns.Count] : row.ItemArray).ToArray()                                      })                        ).SelectMany(s => s);         [... create a DataTable with the resulting left join, etc. ...]   } 

Note: the variable, ‘flatRow’, stores an object array of left joined data; it is added to a DataRowCollection later in the method (not shown).

Snippet 2 (not working; no errors thrown, however):

var dtBase = ds.Tables[0].AsEnumerable();   for (int i = 1; i < ds.Tables.Count; i++) {       var query = dtBase.GroupJoin(ds.Tables[i].AsEnumerable(),                                  or => KeySelector(or, JoinKey),                                    ir => KeySelector(ir, JoinKey),                                  (baseRow, joinRows) => joinRows.DefaultIfEmpty()                                      .Select(joinRow => new                                      {                                          flatRow = baseRow.ItemArray.Concat((joinRow == null) ? new object[ds.Tables[i].Columns.Count] : joinRow.ItemArray).ToArray()                                      })                                  )                        .SelectMany(s => s);      [... create a DataTable with the resulting left join, etc. ...]   } 

Here is the KeySelector function used above (please see the comments inline):

private IEnumerable KeySelector(DataRow dr, params JoinKey[] JoinKey) {   List<object> gl = new List<object>();   foreach (JoinKey jk in JoinKey)   {     // note that I did try to mimic the 'hard-coded' approach from Snippet 1:     // this does not work:     // gl.Add(dr.Field<jk.DataType>(jk.Name)); --> it does not like <jk.DataType>      // I 'hacked' around it by using the following:      gl.Add(dr[dr.Table.Columns.IndexOf(jk.Name)]);   }   return gl; } 

Snippet 2 only returns data from DataTable[0]. None of the data from DataTable[1-n] (if any exists) is concatenated into the variable, ‘flatRow.’ Interestingly, though, I do get the correct number of elements in the array. I know this has something to do with the KeySelector method, but (obviously) I have no idea what the issue is.

Please let me know if anyone needs additional information. Your help is greatly appreciated…

Thanks,
Tyler

  • 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. 2026-05-10T23:11:52+00:00Added an answer on May 10, 2026 at 11:11 pm

    One thing that immediately occurs is that using a double in a key is asking for trouble; equality comparisons on floating point numbers are notoriously flakey. I’m still looking, though.

    I believe the main problem here is that there is simply no automatic equality on lists based on contents; you are returning different lists (from KeySelector), so they simply never equal eachother. You might be able to provide a custom comparer…

    This is messy, but gets something returning:

        class SetComparer : IEqualityComparer<IEnumerable>     {          public readonly static SetComparer Default = new SetComparer();          public bool Equals(IEnumerable x, IEnumerable y)         {             return Enumerable.SequenceEqual(x.Cast<object>(), y.Cast<object>());         }          public int GetHashCode(IEnumerable data)         {             int hash = 0;             foreach (object obj in data)             {                 if (obj != null)                 {                     hash = hash * 7 + 13 * obj.GetHashCode();                 }             }             return hash;         }     } 

    And pass SetComparer.Default in as the final (optional) arg to GroupJoin.

    Update: found my bug – I’d borked GetHashCode(); fixed.

    You could probably also do something similar by building an expression at runtime for use as the comparer (and just returning the row itself as the key), but this is more complex.

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Look at Assembly Binding Redirection... You can redirect the old… May 11, 2026 at 11:32 pm
  • Editorial Team
    Editorial Team added an answer Use characterAtIndex:. However, this returns a Unicode character (unichar). If… May 11, 2026 at 11:32 pm
  • Editorial Team
    Editorial Team added an answer Flash is very limited to what HTML it can render… May 11, 2026 at 11:32 pm

Related Questions

Given a DataSet, I'm left joining DataTables[1-n] onto DataTable[0]. I have created a method
Given a list of objects, I am needing to transform it into a dataset
I'd like to create a data table given a List using the CopyToDataTable method
I have a winform application that uses some referenced web services to get data.
Is there a relatively easy way to extract a relationship-consistent subset of a DataSet

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.