• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1257
  • Last Modified:

Dataset Datable or Dataview Subset Exclude columns

I have a datatable initiaDT with columns A, B, C, ... H.
I want to have a subset of such datable subsetDt with columns B, D, G
Is there a way to do that w/o looping through every row of initiaDT ?

Same thing applies for a Dataset or Dataview.
0
the_bachelor
Asked:
the_bachelor
  • 3
  • 3
1 Solution
 
Bob LearnedCommented:
Use the DataTable.Copy method to get a copy, and then remove the columns that you don't need.

Bob
0
 
the_bachelorAuthor Commented:
I thought about doing something similar but that didnt seem "elegant" to me.
I was hoping that I'd be able so run some sort of adhoc select on the data (set, table, or view)
0
 
Bob LearnedCommented:
Are you talking about from the database side, or from in-memory DataTable/DataSet?

Bob
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
the_bachelorAuthor Commented:
in/memory Dataset/DataTable/Dataview side
0
 
Bob LearnedCommented:
There isn't any mechanism for a DataTable to select only a specific set of columns, like an SQL query.  The DataTable.Select method returns an array of DataRow instances that match a filter criteria (Name = 'Bob'), but they have all the columns that the source DataTable does.

Bob
0
 
the_bachelorAuthor Commented:
I havent found anything helpful. I guess I'm gunna have to agree with you there LeanedOne.
Maybe I should write my own.
0
 
kainhartCommented:
With the advent of LINQ you can write an expression which would limit down the returned columns within a project much like a SQL select command. When running a LINQ expression on a DataTable one of the options for getting at the results is to use the ToDataTable method which should return a DataTable with the projected columns from your expression. This methodology allows many of hte SQL like SET based commands to be done within C# in a way that is concise and probably more efficient than copying a DataTable and then removing columns.
0
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.

Join & Write a Comment

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.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now