Click here to Skip to main content
15,908,673 members
Home / Discussions / C#
   

C#

 
GeneralRe: String value in C# Pin
Richard MacCutchan5-Jul-11 0:59
mveRichard MacCutchan5-Jul-11 0:59 
AnswerRe: String value in C# Pin
BobJanova5-Jul-11 0:39
BobJanova5-Jul-11 0:39 
GeneralRe: String value in C# Pin
Pierre besquent5-Jul-11 0:47
Pierre besquent5-Jul-11 0:47 
GeneralRe: String value in C# Pin
mabo425-Jul-11 2:45
mabo425-Jul-11 2:45 
GeneralRe: String value in C# Pin
Pierre besquent5-Jul-11 5:55
Pierre besquent5-Jul-11 5:55 
GeneralRe: String value in C# Pin
Richard MacCutchan5-Jul-11 6:35
mveRichard MacCutchan5-Jul-11 6:35 
GeneralRe: String value in C# Pin
BobJanova5-Jul-11 6:02
BobJanova5-Jul-11 6:02 
Questiondynamic group by via LINQ Pin
devvvy4-Jul-11 18:16
devvvy4-Jul-11 18:16 
hi
how can I perform a group by on a DataTable's rows using dynamically determined group-by-columns?

For example, below, I group by "Symbol" and "Measure" - however, what if group by column is passed into the function as argument IList<string> GroupByColumnNames

<br />
static void TestLINQGroupBy()<br />
        {<br />
            #region Construct dummy data<br />
            Random rnd;<br />
            IList<string> Measures = new List<string>() { "eps", "bps", "close" };<br />
            IList<string> Symbols = new List<string>() { "0001HK", "0002HK", "0003HK" };<br />
            DataTable TestPrice = new DataTable("TestPrice");<br />
            DataColumn c = null;<br />
            DataRow newRw = null;<br />
<br />
            DataColumn[] PKs = new DataColumn[3];<br />
            c = new DataColumn("Symbol", typeof(string));<br />
            TestPrice.Columns.Add(c);<br />
            PKs[0] = c;<br />
<br />
            c = new DataColumn("Measure", typeof(string));<br />
            TestPrice.Columns.Add(c);<br />
            PKs[1] = c;<br />
<br />
            c = new DataColumn("RecordDate", typeof(DateTime));<br />
            TestPrice.Columns.Add(c);<br />
            PKs[2] = c;<br />
            <br />
            TestPrice.Columns.Add("Value", typeof(double));<br />
            TestPrice.PrimaryKey = PKs;<br />
            <br />
            foreach (string Symbol in Symbols)<br />
            {<br />
                foreach (string Measure in Measures)<br />
                {<br />
                    for (int iter = 1; iter < 12; iter++)<br />
                    {<br />
                        newRw = TestPrice.NewRow();<br />
                        newRw["Symbol"] = Symbol;<br />
                        newRw["Measure"] = Measure;<br />
                        rnd = new Random((Symbol + Measure + iter).GetHashCode());<br />
                        newRw["Value"] = rnd.NextDouble();<br />
                        newRw["RecordDate"] = new DateTime(DateTime.Now.Year, iter, 1);<br />
                        TestPrice.Rows.Add(newRw);<br />
                    }<br />
                }<br />
            }<br />
            #endregion<br />
<br />
            var q = from rw in TestPrice.Select()<br />
                    group rw by new {Key1=rw["Symbol"], Key2=rw["Measure"]} into key<br />
                    select key;<br />
            foreach (var k in q)<br />
            {<br />
                Console.WriteLine(k);<br />
            }<br />
            return;<br />
        }<br />


REF: 101 LINQ Examples[^]
dev

AnswerRe: dynamic group by via LINQ Pin
Abhinav S4-Jul-11 20:30
Abhinav S4-Jul-11 20:30 
QuestionUnmanaged dll call from C#, AccessViolationExceptionMessage [modified] Pin
marca2923-Jul-11 22:24
marca2923-Jul-11 22:24 
AnswerRe: Unmanaged dll call from C#, AccessViolationExceptionMessage Pin
Richard MacCutchan3-Jul-11 22:37
mveRichard MacCutchan3-Jul-11 22:37 
GeneralRe: Unmanaged dll call from C#, AccessViolationExceptionMessage Pin
marca2923-Jul-11 22:47
marca2923-Jul-11 22:47 
GeneralRe: Unmanaged dll call from C#, AccessViolationExceptionMessage Pin
Richard MacCutchan3-Jul-11 23:22
mveRichard MacCutchan3-Jul-11 23:22 
GeneralRe: Unmanaged dll call from C#, AccessViolationExceptionMessage Pin
marca2923-Jul-11 23:26
marca2923-Jul-11 23:26 
AnswerRe: Unmanaged dll call from C#, AccessViolationExceptionMessage Pin
BobJanova4-Jul-11 0:38
BobJanova4-Jul-11 0:38 
AnswerRe: Unmanaged dll call from C#, AccessViolationExceptionMessage Pin
Pete O'Hanlon4-Jul-11 1:15
mvePete O'Hanlon4-Jul-11 1:15 
AnswerRe: Unmanaged dll call from C#, AccessViolationExceptionMessage Pin
George Tryfonas5-Jul-11 3:33
George Tryfonas5-Jul-11 3:33 
Questiondatabase connection code in c# for asp .net Pin
Arvind Vishwakarma2-Jul-11 6:09
Arvind Vishwakarma2-Jul-11 6:09 
AnswerRe: database connection code in c# for asp .net Pin
Richard MacCutchan2-Jul-11 6:47
mveRichard MacCutchan2-Jul-11 6:47 
AnswerRe: database connection code in c# for asp .net Pin
PIEBALDconsult2-Jul-11 6:54
mvePIEBALDconsult2-Jul-11 6:54 
AnswerRe: database connection code in c# for asp .net Pin
RaviRanjanKr2-Jul-11 8:34
professionalRaviRanjanKr2-Jul-11 8:34 
AnswerRe: database connection code in c# for asp .net Pin
Abhinav S2-Jul-11 23:33
Abhinav S2-Jul-11 23:33 
Questionhow to "inject" string Pin
igalep1322-Jul-11 5:53
igalep1322-Jul-11 5:53 
AnswerRe: how to "inject" string Pin
PIEBALDconsult2-Jul-11 6:06
mvePIEBALDconsult2-Jul-11 6:06 
AnswerRe: how to "inject" string Pin
Mark Salsbery2-Jul-11 6:09
Mark Salsbery2-Jul-11 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.