PC Review


Reply
Thread Tools Rate Thread

Creating a DataTable from another DataTable

 
 
nltoft
Guest
Posts: n/a
 
      7th Mar 2009
I would like to create a DataTable from another DataTable so that I can sum
certain fields using a GROUP BY clause. Is this possible?

For example:
Table1:
Employee, Department, Hours (these are the field names)
Emp1, Dept1, 8 (first row of data)
Emp1, Dept1, 8
Emp1, Dept2, 8
Emp1, Dept1, 8
Emp1, Dept2, 8
Emp2, etc.

I want to create a Table2 from Table1 using:
SELECT Employee, Department, SUM(Hours) as TotHrs FROM Table1
GROUP BY Employee, Department

So for Emp1, Table2 would contain two rows:
Employee, Department, TotHrs
Emp1, Dept1, 24
Emp1, Dept2, 16

If this is not possible, is there some way to achieve the same effect?
 
Reply With Quote
 
 
 
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      7th Mar 2009
Hi,

The select has definitly been a reason why LINQ to SQL is made. Some expect
the Select is a kind of mini SQL, it is not.

Have simple a look at Linq to SQL for the program language you are using.

A select returns a collection of datarows, by importing them again, and then
using one of the overloaded methods of the dataview will probably give you
the result you want, but this can better be done with Linq to SQL

Cor

"nltoft" <(E-Mail Removed)> wrote in message
newsAFDB611-1199-4C28-AB66-(E-Mail Removed)...
>I would like to create a DataTable from another DataTable so that I can sum
> certain fields using a GROUP BY clause. Is this possible?
>
> For example:
> Table1:
> Employee, Department, Hours (these are the field names)
> Emp1, Dept1, 8 (first row of data)
> Emp1, Dept1, 8
> Emp1, Dept2, 8
> Emp1, Dept1, 8
> Emp1, Dept2, 8
> Emp2, etc.
>
> I want to create a Table2 from Table1 using:
> SELECT Employee, Department, SUM(Hours) as TotHrs FROM Table1
> GROUP BY Employee, Department
>
> So for Emp1, Table2 would contain two rows:
> Employee, Department, TotHrs
> Emp1, Dept1, 24
> Emp1, Dept2, 16
>
> If this is not possible, is there some way to achieve the same effect?


 
Reply With Quote
 
Miha Markic
Guest
Posts: n/a
 
      7th Mar 2009
Hi,

There are several solutions:
- check out QueryADataset library (http://www.queryadataset.com/) to perform
SQL commands on in-memory table
- use Linq to Dataset to group the data from in-memory table
- execute query on server and avoid client side code
....

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: blog.rthand.com

"nltoft" <(E-Mail Removed)> wrote in message
newsAFDB611-1199-4C28-AB66-(E-Mail Removed)...
> I would like to create a DataTable from another DataTable so that I can
> sum
> certain fields using a GROUP BY clause. Is this possible?
>
> For example:
> Table1:
> Employee, Department, Hours (these are the field names)
> Emp1, Dept1, 8 (first row of data)
> Emp1, Dept1, 8
> Emp1, Dept2, 8
> Emp1, Dept1, 8
> Emp1, Dept2, 8
> Emp2, etc.
>
> I want to create a Table2 from Table1 using:
> SELECT Employee, Department, SUM(Hours) as TotHrs FROM Table1
> GROUP BY Employee, Department
>
> So for Emp1, Table2 would contain two rows:
> Employee, Department, TotHrs
> Emp1, Dept1, 24
> Emp1, Dept2, 16
>
> If this is not possible, is there some way to achieve the same effect?


 
Reply With Quote
 
nltoft
Guest
Posts: n/a
 
      9th Mar 2009
Thank you and Miha very much. Since you are an MVP, could you please direct
me to which forum I should ask a question about a project building
unexpectedly within one of my solutions? I've tried 3 forums, but no response
in any of them yet (Project management, C sharp general discussion, Visual
Studio .NET general discussion).

"Cor Ligthert[MVP]" wrote:

> Hi,
>
> The select has definitly been a reason why LINQ to SQL is made. Some expect
> the Select is a kind of mini SQL, it is not.
>
> Have simple a look at Linq to SQL for the program language you are using.
>
> A select returns a collection of datarows, by importing them again, and then
> using one of the overloaded methods of the dataview will probably give you
> the result you want, but this can better be done with Linq to SQL
>
> Cor
>
> "nltoft" <(E-Mail Removed)> wrote in message
> newsAFDB611-1199-4C28-AB66-(E-Mail Removed)...
> >I would like to create a DataTable from another DataTable so that I can sum
> > certain fields using a GROUP BY clause. Is this possible?
> >
> > For example:
> > Table1:
> > Employee, Department, Hours (these are the field names)
> > Emp1, Dept1, 8 (first row of data)
> > Emp1, Dept1, 8
> > Emp1, Dept2, 8
> > Emp1, Dept1, 8
> > Emp1, Dept2, 8
> > Emp2, etc.
> >
> > I want to create a Table2 from Table1 using:
> > SELECT Employee, Department, SUM(Hours) as TotHrs FROM Table1
> > GROUP BY Employee, Department
> >
> > So for Emp1, Table2 would contain two rows:
> > Employee, Department, TotHrs
> > Emp1, Dept1, 24
> > Emp1, Dept2, 16
> >
> > If this is not possible, is there some way to achieve the same effect?

>
>

 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      9th Mar 2009
As it is about C# then the most general newsgroup despite its name is

microsoft.public.dotnet.languages.csharp


"nltoft" <(E-Mail Removed)> wrote in message
news:04178715-ED90-4B16-862F-(E-Mail Removed)...
> Thank you and Miha very much. Since you are an MVP, could you please
> direct
> me to which forum I should ask a question about a project building
> unexpectedly within one of my solutions? I've tried 3 forums, but no
> response
> in any of them yet (Project management, C sharp general discussion, Visual
> Studio .NET general discussion).
>
> "Cor Ligthert[MVP]" wrote:
>
>> Hi,
>>
>> The select has definitly been a reason why LINQ to SQL is made. Some
>> expect
>> the Select is a kind of mini SQL, it is not.
>>
>> Have simple a look at Linq to SQL for the program language you are using.
>>
>> A select returns a collection of datarows, by importing them again, and
>> then
>> using one of the overloaded methods of the dataview will probably give
>> you
>> the result you want, but this can better be done with Linq to SQL
>>
>> Cor
>>
>> "nltoft" <(E-Mail Removed)> wrote in message
>> newsAFDB611-1199-4C28-AB66-(E-Mail Removed)...
>> >I would like to create a DataTable from another DataTable so that I can
>> >sum
>> > certain fields using a GROUP BY clause. Is this possible?
>> >
>> > For example:
>> > Table1:
>> > Employee, Department, Hours (these are the field names)
>> > Emp1, Dept1, 8 (first row of data)
>> > Emp1, Dept1, 8
>> > Emp1, Dept2, 8
>> > Emp1, Dept1, 8
>> > Emp1, Dept2, 8
>> > Emp2, etc.
>> >
>> > I want to create a Table2 from Table1 using:
>> > SELECT Employee, Department, SUM(Hours) as TotHrs FROM Table1
>> > GROUP BY Employee, Department
>> >
>> > So for Emp1, Table2 would contain two rows:
>> > Employee, Department, TotHrs
>> > Emp1, Dept1, 24
>> > Emp1, Dept2, 16
>> >
>> > If this is not possible, is there some way to achieve the same effect?

>>
>>


 
Reply With Quote
 
Miha Markic
Guest
Posts: n/a
 
      9th Mar 2009


"nltoft" <(E-Mail Removed)> wrote in message
news:04178715-ED90-4B16-862F-(E-Mail Removed)...
> a question about a project building
> unexpectedly within one of my solutions?


What do you mean?

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: blog.rthand.com

 
Reply With Quote
 
nltoft
Guest
Posts: n/a
 
      9th Mar 2009
Ah, you're an MVP, too. Since you asked, , and I did already post this in
the C# forum with no response:

I have a .NET C# solution with 6 projects as follows:

Project A (DLL) not dependent on any other projects
Project B (DLL) dependent on Project A
Project C (DLL) dependent on Project A and B
Project D (DLL) dependent on Project A and B
Project E (EXE) dependent on Project A, B, C, D
Project F (setup project)

I checked all project dependencies by right clicking the projects in the
Solution Explorer and they look good. However, when I make a change to a
class in Project D only, Projects B and C rebuild and I expect them not to.
Project A does not rebuild, as expected. Projects E and F do rebuild, but
that is also expected.

Can anyone explain this? I do not want Projects B and C to change timestamp
if there is no reason for them to do so. I do not want to be forced to give
them new build numbers when nothing has really changed.


"Miha Markic" wrote:

>
>
> "nltoft" <(E-Mail Removed)> wrote in message
> news:04178715-ED90-4B16-862F-(E-Mail Removed)...
> > a question about a project building
> > unexpectedly within one of my solutions?

>
> What do you mean?
>
> --
> Miha Markic [MVP C#, INETA Country Leader for Slovenia]
> RightHand .NET consulting & development www.rthand.com
> Blog: blog.rthand.com
>

 
Reply With Quote
 
Miha Markic
Guest
Posts: n/a
 
      11th Mar 2009
Hi,

How are projects dependant? Through references or just through
configuration?
I created a sample like you've described and linked the projects through
references (no manual dependencies were added) and it behaves like you or I
would expect.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: blog.rthand.com

"nltoft" <(E-Mail Removed)> wrote in message
news:ACEDD09D-FABB-45B1-8BAA-(E-Mail Removed)...
> Ah, you're an MVP, too. Since you asked, , and I did already post this
> in
> the C# forum with no response:
>
> I have a .NET C# solution with 6 projects as follows:
>
> Project A (DLL) not dependent on any other projects
> Project B (DLL) dependent on Project A
> Project C (DLL) dependent on Project A and B
> Project D (DLL) dependent on Project A and B
> Project E (EXE) dependent on Project A, B, C, D
> Project F (setup project)
>
> I checked all project dependencies by right clicking the projects in the
> Solution Explorer and they look good. However, when I make a change to a
> class in Project D only, Projects B and C rebuild and I expect them not
> to.
> Project A does not rebuild, as expected. Projects E and F do rebuild, but
> that is also expected.
>
> Can anyone explain this? I do not want Projects B and C to change
> timestamp
> if there is no reason for them to do so. I do not want to be forced to
> give
> them new build numbers when nothing has really changed.
>
>
> "Miha Markic" wrote:
>
>>
>>
>> "nltoft" <(E-Mail Removed)> wrote in message
>> news:04178715-ED90-4B16-862F-(E-Mail Removed)...
>> > a question about a project building
>> > unexpectedly within one of my solutions?

>>
>> What do you mean?
>>
>> --
>> Miha Markic [MVP C#, INETA Country Leader for Slovenia]
>> RightHand .NET consulting & development www.rthand.com
>> Blog: blog.rthand.com
>>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataTable -> DataView -> Sort -> DataGrid : need related DataTable index Diamonds Microsoft ADO .NET 4 28th Jun 2006 08:23 PM
what is the best way to make a DataTable thread safe? DataTable.AcceptChanges() will throw exception (not documented) Ryan Liu Microsoft C# .NET 3 7th Jun 2006 05:07 PM
Retrieving datarows using DataTable.Select method and adding it to new DataTable C.Anand via DotNetMonster.com Microsoft ADO .NET 4 4th Apr 2005 05:21 PM
Creating a Queried DataTable From an Existing DataTable Lee Ottaway Microsoft ADO .NET 1 22nd Jul 2004 09:30 AM
Creating a new Datatable with from an existing Datatable using SQL monkhi Microsoft ADO .NET 1 16th Mar 2004 07:52 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:38 PM.