Domanda

Consider the following situation:

public class Employee
{
    public string Name {get; set}
    public string Email {get; set}
}

public class EnployeeGroup
{
    //List of employees in marketting
    public IList<Employee> MarkettingEmployees{ get; }

    //List of employees in sales
    public IList<Employee> SalesEmployees{ get; }
}

private EnployeeGroup GroupA;

int MarkettingCount;
string MarkettingNames;

MarkettingCount = GroupA.MarkettingEmployees.Count; //assigns MarkettingCount=5, this will always be 5-10 employees
MarkettingNames = <**how can i join all the GroupA.MarkettingEmployees.Name into a comma separated string?** >

//I tried a loop:
foreach(Employee MktEmployee in GroupA.MarkettingEmployees)
{
    MarkettingNames += MktEmployee.Name + ", ";
}

The loop works, but i want to know:

  1. Is Looping the most efficient/elegant way of doing this? If not then what are the better alternatives? I tried string.join but couldnt get it working..
  2. I want to avoid Linq..
È stato utile?

Soluzione

You need a little bit of LINQ whether you like it or not ;)

MarkettingNames = string.Join(", ", GroupA.MarkettingEmployees.Select(e => e.Name));

Altri suggerimenti

From a practicality standpoint, there's no reasonable argument for avoiding a loop. Iterations are at the hard of every general-purpose programming language.

Using LINQ is elegant in simple cases. Again, there's no sound reason to avoid it per se.

In case you are looking for a rather obscure, academic solution, there's always tail recursion. However, your data structure would have to be adapted for it. Note that even if you use it, a smart compiler will detect it and optimize into a loop. The odds are agains you!

As an alternative you could use a StringBuilder with Append instead of creating a new string at each iteration

This would be much more efficient (see caveat below):

var stringBuilder = new StringBuilder();
foreach (Employee MktEmployee in GroupA.MarkettingEmployees)
{
    stringBuilder.Append(MktEmployee.Name + ", ");
}

Then this:

foreach(Employee MktEmployee in GroupA.MarkettingEmployees)
{
    MarkettingNames += MktEmployee.Name + ", ";
}

Edit: If you were to have a large amount of employees this would be much more efficient. However, a trivial loop of 5-10 is actually slightly less efficient.

In small cases - this isn't going to be that large of a hit on performance, but in large cases the pay off will be significant.

Also, if you are to use the explicit loop approach, it's probably best to trim off that last ", " by using something like:

myString = myString.Trim().TrimEnd(',');

The article below explains when you should use StringBuilder to concatenate strings.

In short, in the approach you use: the concatenation is creating a new string each time, which obviously eats up a lot of memory. You also need to copy all the data from the existing string of MarkettingNames to the new string being appended yet another MktEmployee.Name + ", ".

Thank you, Jon Skeet: http://www.yoda.arachsys.com/csharp/stringbuilder.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top