1

I have a list of users returned in a class

My Class: List

public class EmployeeList
{
    public int Emp_No { get; set; }
    public string Sys_Prefix { get; set; }
    public short Company_No { get; set; }
    public string Surname { get; set; }
    public string First_Name { get; set; }
    public string Known_As { get; set; }
    public string Initials { get; set; }
    public string Title { get; set; }
}

without looping through the results of this class i want to add the results to a new list var newsFeedEmps = new List<NewsFeedEmployees>();

public partial class NewsFeedEmployees
{
    public long Company_No { get; set; }
    public long Emp_No { get; set; }
    public long SourceId { get; set; }
    public string Sys_Prefix { get; set; }

    public long NewsFeedID { get; set; }

    public bool isActive { get; set; }
}

I cant use newsFeedEmps.AddRange(EmployeeList) because not all the parameters match and i will be assigning a separate value to NewsFeedID and isActive for the full list

I don't want to use a foreach loop to loop through the first list and do a newsFeedEmps.Add(new .... ) I found a separate post mentioning

Queue<NewsFeedEmployees> myQueue = new Queue<NewsFeedEmployees>();
myQueue.EnqueueRange(emps.Select(emp => new NewsFeedEmployees()
{
    Company_No = emp.Company_No,
    Emp_No = emp.Emp_No,
}));

But i get an error that Queue does not contain a definition for EnqueueRange

10
  • 2
    Why without looping? Copying one list to another is inherently an O(n) operation. Commented Feb 15 at 12:09
  • You could use LINQ: var newList = oldList.Select(...).ToList();, but internally that's using loops anyway. Commented Feb 15 at 12:11
  • 1
    "Queue does not contain a definition for EnqueueRange" - what made you think it would?
    – Fildor
    Commented Feb 15 at 12:16
  • 1
    But back to your question: "No looping" - does that actually mean "no loops, please" or just "please hide those pesky loops behind an api for me"?
    – Fildor
    Commented Feb 15 at 13:29
  • 2
    I mean you could create an experience like this: dotnetfiddle.net/CWp9LH which still uses loops. Just that these are "hidden" from you as the consumer of the API that is created by the extensions.
    – Fildor
    Commented Feb 15 at 13:39

2 Answers 2

0

You can do this in various ways without an explicit for loop, but if the goal is

trying to reduce time taken

I'm quite confident that you can't do better than O(n) (actually θ(n)).

Here's a way you can avoid an explicit for loop. If you want to go with something already built in, you can write the operation like this:

Queue<NewsFeedEmployees> myQueue = new Queue<NewsFeedEmployees>();
emps.Select(emp => new NewsFeedEmployees
    {
        Company_No = emp.Company_No,
        Emp_No = emp.Emp_No,
    })
    .ToList()
    .ForEach(myQueue.Enqueue);

Unfortunately, ForEach is only available on List<T>, which is why ToList() is required.

This actually loops through emps twice, but still has θ(n) running time.

You might think, then, that you could write your own ForEach extension method directly on IEnumerable<T>, and while you can, as Eric Lippert explains, there's not much point in doing that.

If, however, we assume that emps is already a List<EmployeeList> you could also do this:

Queue<NewsFeedEmployees> myQueue = new Queue<NewsFeedEmployees>();
emps.ForEach(emp => myQueue.Enqueue(new NewsFeedEmployees
    {
        Company_No = emp.Company_No,
        Emp_No = emp.Emp_No,
    }));

This, at least, only implicitly loops through emps once. It's still θ(n), although it's theoretically twice as fast as the previous suggestion.

In any case, List<T>.ForEach loops through the list once, and I feel confident stating that you can't do better than that.

In a language like F# or Haskell, one could implement the same kind of operation with recursion instead of explicit loops, but it would still be an θ(n) operation.

Ultimately, though, unless you have millions of records, don't worry. As Rob Pike stated,

n is usually small.

0

-> In Your Case, Without using foreach loop, you can use method to each item to convert in EmployeeList to NewsFeedEmployees object. and create new List from result.

-> Follow my code to solve your problem.

Code :-

List<EmployeeList> employees = /* your list of EmployeeList */;
List<NewsFeedEmployees> newsFeedEmps = employees.Select(emp => new 
 NewsFeedEmployees
 {
    Company_No = emp.Company_No,
    Emp_No = emp.Emp_No,
    // Assign values to other properties as needed
    SourceId = 0, // Example value, replace with actual value
    NewsFeedID = 0, // Example value, replace with actual value
    isActive = true // Example value, replace with actual value
  }).ToList();

Not the answer you're looking for? Browse other questions tagged or ask your own question.