Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Linq: datatable queries

Posted on 2011-10-06
Medium Priority
Last Modified: 2012-05-12
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

Question by:acadenilla
  • 2
  • 2
LVL 96

Expert Comment

by:Bob Learned
ID: 36930908
It seems like you have everything that you need.  What kind of help do you need?

Author Comment

ID: 36931360

Peer code review i guess.

Curious to know if anybody had better suggestion.

How would you have done it?


LVL 96

Accepted Solution

Bob Learned earned 1600 total points
ID: 36931370
How would I have done it?  First, no DataTable, second, NHibernate, third, I wouldn't have done this kind of manipulation at this point...
LVL 16

Assisted Solution

disrupt earned 400 total points
ID: 36931436
There is a merge method you can use take a look at this:


Author Closing Comment

ID: 36932035
thanks guys for the comments.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question