Click here to Skip to main content
15,894,460 members
Home / Discussions / C#
   

C#

 
AnswerRe: Update User Environment Variable Pin
OriginalGriff2-Apr-19 5:50
mveOriginalGriff2-Apr-19 5:50 
GeneralRe: Update User Environment Variable Pin
DavPCode2-Apr-19 7:12
DavPCode2-Apr-19 7:12 
GeneralRe: Update User Environment Variable Pin
Richard Deeming2-Apr-19 8:06
mveRichard Deeming2-Apr-19 8:06 
GeneralRe: Update User Environment Variable Pin
DavPCode2-Apr-19 12:07
DavPCode2-Apr-19 12:07 
QuestionValues Sum in a file Pin
Member 1420343029-Mar-19 10:46
Member 1420343029-Mar-19 10:46 
AnswerRe: Values Sum in a file Pin
OriginalGriff29-Mar-19 21:21
mveOriginalGriff29-Mar-19 21:21 
AnswerRe: Values Sum in a file Pin
Member 1420343030-Mar-19 3:29
Member 1420343030-Mar-19 3:29 
AnswerRe: Values Sum in a file Pin
BillWoodruff31-Mar-19 19:25
professionalBillWoodruff31-Mar-19 19:25 
It's good you got your code working !

I am going to respond with an example of using Linq that, imho, is valuable/educational because it illustrates using an index in a 'Select statement, dealing with the fact there is no equivalent to 'continue, or 'break, in a 'Select statement, and using 'GroupBy and 'ToDictionary to perform summation of the 'Value part of an 'IGroup:
C#
string s = @"d1.8xlarge: 3 d1.xlarge: 0 c2.xlarge: 25
s1.medium: 1 c2.xlarge: 10 s1.large: 4 d1.8xlarge: 27";

string[] split = s.Split(new char[] {' ',':', '\r','\n'}, StringSplitOptions.RemoveEmptyEntries);

int limit = split.Length - 1;

var sorted = split.Select((string str, int ndx) => // declaration of auto-index
{
    int val;  // use of variable defined inside 'Select

    if (ndx < limit && int.TryParse(split[ndx + 1], out val))
    {
        return new {name = split[ndx], value = val};
    }
    else
    {
        return null; // you have to return something !
    }
})
.Where(itm => itm != null) // get rid of 'nulls
.GroupBy(itm => itm.name) // group on the 'name
.ToDictionary(grp => grp.Key, grp => grp.Sum(v => v.value)); // summation
I'm not claiming this is the best solution ! But, imho, the aspects of using Linq it demonstrates are very useful in many other situations.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

QuestionSplit lines in a file Pin
Member 1420343029-Mar-19 8:34
Member 1420343029-Mar-19 8:34 
AnswerRe: Split lines in a file Pin
OriginalGriff29-Mar-19 9:11
mveOriginalGriff29-Mar-19 9:11 
QuestionException while trying reading a local file using online compiler Pin
Member 1420343029-Mar-19 6:28
Member 1420343029-Mar-19 6:28 
AnswerRe: Exception while trying reading a local file using online compiler Pin
OriginalGriff29-Mar-19 6:42
mveOriginalGriff29-Mar-19 6:42 
GeneralRe: Exception while trying reading a local file using online compiler Pin
Member 1420343029-Mar-19 7:19
Member 1420343029-Mar-19 7:19 
GeneralRe: Exception while trying reading a local file using online compiler Pin
OriginalGriff29-Mar-19 7:23
mveOriginalGriff29-Mar-19 7:23 
GeneralRe: Exception while trying reading a local file using online compiler Pin
Dave Kreskowiak29-Mar-19 9:44
mveDave Kreskowiak29-Mar-19 9:44 
GeneralRe: Exception while trying reading a local file using online compiler Pin
Member 1420343029-Mar-19 10:48
Member 1420343029-Mar-19 10:48 
QuestionGetting exception message "Incorrect syntax near '(' " in C# web app Pin
Member 1420265628-Mar-19 20:10
Member 1420265628-Mar-19 20:10 
AnswerRe: Getting exception message "Incorrect syntax near '(' " in C# web app Pin
GenJerDan28-Mar-19 20:58
GenJerDan28-Mar-19 20:58 
AnswerRe: Getting exception message "Incorrect syntax near '(' " in C# web app Pin
OriginalGriff28-Mar-19 21:04
mveOriginalGriff28-Mar-19 21:04 
AnswerRe: Getting exception message "Incorrect syntax near '(' " in C# web app PinPopular
Luc Pattyn28-Mar-19 23:39
sitebuilderLuc Pattyn28-Mar-19 23:39 
GeneralRe: Getting exception message "Incorrect syntax near '(' " in C# web app Pin
OriginalGriff28-Mar-19 23:42
mveOriginalGriff28-Mar-19 23:42 
GeneralRe: Getting exception message "Incorrect syntax near '(' " in C# web app Pin
Luc Pattyn28-Mar-19 23:47
sitebuilderLuc Pattyn28-Mar-19 23:47 
GeneralRe: Getting exception message "Incorrect syntax near '(' " in C# web app Pin
GenJerDan29-Mar-19 1:39
GenJerDan29-Mar-19 1:39 
GeneralRe: Getting exception message "Incorrect syntax near '(' " in C# web app Pin
Eddy Vluggen29-Mar-19 2:56
professionalEddy Vluggen29-Mar-19 2:56 
QuestionDebugging + MSTest + Multiple Startup Project question Pin
Super Lloyd28-Mar-19 19:59
Super Lloyd28-Mar-19 19:59 

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.