0

Is there a more efficient way to handle this?

List<String> lstReferences = (from f in 
                            (from section in courseSectionToCreate.SectionsToAdd
                                select new {
                                            ReferenceNumber = section.Course.CourseNumber.Substring(0, 5) + "." +
                                                section.Course.CourseNumber.Substring(5) + "." +
                                                section.Session + "." +
                                                section.Year + "." +
                                                section.SectionNumber + ";"
                                            })
                            select f.ReferenceNumber).ToList();

strReferenceNumber = lstReferences.Aggregate((a, b) => a + ", " + b);
0

3 Answers 3

2

Yes, you definitely don't want to be using Aggregate here. That is O(n^2) (it's Schlemiel the Painter's algorithm). Instead:

string referenceNumber = String.Join(", ", lstReferences);

This is better because String.Join will use a StringBuilder internally.

Sign up to request clarification or add additional context in comments.

Comments

0

You can replace all that with this:

var strReferenceNumber =
    String.Join(", ",
        courseSectionToCreate.SectionsToAdd.Select(s =>
            String.Join(".", 
                s.Course.CourseNumber.Substring(0, 5),
                s.Course.CourseNumber.Substring(5),
                s.Session,
                s.Year,
                s.SectionNumber) + ";"
        )
    );

1 Comment

Thanks for this answer, it appears to be processing the fastest on my machine.
0

How about:

var lstReferences = from section in courseSectionToCreate.SectionsToAdd     
                    let courseNumber = section.Course.CourseNumber                           
                    let toJoin = new object[] 
                    { 
                       courseNumber.Substring(0, 5),
                       courseNumber.Substring(5),
                       section.Session,
                       section.Year,
                       section.SectionNumber
                    }
                    select string.Join(".", toJoin) + ";" 

var strReferenceNumber = string.Join(", ", lstReferences); 

10 Comments

I doubt (but don't know) that that will cooperate with LINQ to SQL (or other queryable providers like that). That is an important consideration.
@Jason: Is this LINQ to SQL? Doesn't look like it, though I could be wrong.
@Jason: It could be. Was an impression; there doesn't appear to be a data-context.
@Jason: LINQ to SQL can definitely handle the string methods used here (String.Join(), String.Concat() and String.Substring()) so it's not a problem.
@Jeff M: Are you sure about String.Join, especially the way that it is being invoked?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.