Linq: datatable queries

Given two datatables

Table1 (Id, etc)
Table2(id, prod, etc)

I want to merge the two table in such a way that will add a column to table1 "prod" that contains a comma delimited list of values from prod 2

While the code attach gets me the desired result.

Does anybody have a better suggestion on how to do this?

   
DataTable dtDealers = ds.Tables[DTDEALERLIST].Copy();
dtDealers.Columns.Add("Products", typeof (string));

var res = from d in ds.Tables[DTDEALERLIST].AsEnumerable()
                                  join p in ds.Tables[DTPRODUCTS].AsEnumerable() on d.Field<int>("dealerList_Id") equals p.Field<int>("dealerList_Id") into r
                                  from p in r.DefaultIfEmpty()
                                  select new
                                             {
                                                 ID = p.Field<int>("dealerList_Id"),
                                                 List = r.ToList().Aggregate(new StringBuilder(), (sb, l) => sb.Append(l.Field<string>("products_Text")).Append(","), sb =>
                                                                                                                                                                          {
                                                                                                                                                                              if (sb.Length > 0) sb.Length--;
                                                                                                                                                                              return sb.ToString();
                                                                                                                                                                          })
                                             };

                        var dis = res.Distinct();

                        foreach(var d in dis)
                        {
                            dtDealers.Rows[d.ID]["Products"] = d.List;
                        }

Open in new window

acadenillaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
It seems like you have everything that you need.  What kind of help do you need?
0
acadenillaAuthor Commented:
TheLearnedOne,

Peer code review i guess.

Curious to know if anybody had better suggestion.

How would you have done it?

Thanks

0
Bob LearnedCommented:
How would I have done it?  First, no DataTable, second, NHibernate, third, I wouldn't have done this kind of manipulation at this point...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
disruptCommented:
There is a merge method you can use take a look at this:

http://msdn.microsoft.com/en-us/library/system.data.datatable.merge.aspx
0
acadenillaAuthor Commented:
thanks guys for the comments.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.