Edward Tanguay
Edward Tanguay

Reputation: 193312

How to remap properties in LINQ?

I am getting a list of running processes via LINQ.

The two properties I want are "ProcessName" and "WorkingSet64".

However, in my UI, I want these properties to be called "ProcessName" and "ProcessId".

How can I remap the name "WorkingSet64" to "ProcessId"?

I did a pseudo syntax of what I am looking for below:

using System.Linq;
using System.Windows;
using System.Diagnostics;

namespace TestLinq22
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            var theProcesses =
                from p in Process.GetProcesses()
                orderby p.WorkingSet64 descending
                select new { p.ProcessName, p.WorkingSet64 alias "ProcessId" };

            foreach (var item in theProcesses)
            {
                TheListBox.Items.Add(item);
            }
        }
    }
}

Upvotes: 1

Views: 601

Answers (2)

Aaron Powell
Aaron Powell

Reputation: 25097

var theProcesses = from p in Process.GetProcesses()
                   orderby p.WorkingSet64 descending
                   select new { p.ProcessName, ProcessId = p.WorkingSet64 };

When you create an annonymous object if you provide the property name it'll use that, otherwise it'll use the name of the property you selected from.

Upvotes: 2

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422016

select new { p.ProcessName, ProcessId = p.WorkingSet64 };

Upvotes: 5

Related Questions