dogeter
dogeter

Reputation: 41

How to fill a property of an object from another list using LINQ

I have 2 lists of objects. One has values, the other has names and values. I want to look up the values in the other list and write the result to pref.Name. I know I can do this with a foreach, but I assume there's a good way to do it in LINQ. Ideas?

public class Preference
    {
        public string Category { get; set; }
        public string Name { get; set; }
        public string ItemValue { get; set; }
        public int SortOrder { get; set; }
    }

public class SAPReadOnlyItem
    {
        public string Category { get; set; }
        public string Name { get; set; }
        public string ItemValue { get; set; }
    }

    List<Preference> preferences = getExistingUserPreferences(UserID, ddlCategory.SelectedValue); //this list is just keys
    List<SAPReadOnlyItem> sapReadOnlyItems = getSAPReadOnlyItems(ddlCategory.SelectedValue); //this list is names and keys

    //i want to look up the name from sapReadOnly using the ID from preferences and write it into preferences[n].Name

    //this works, but I want to write it into preferences[n].Name
    var foobar = (from sap in sapReadOnlyItems
                  join pref in preferences 
                  on sap.ItemValue equals pref.ItemValue
                  select new { asdf = sap.Name }).FirstOrDefault(); //instead of "select new" I want to write it into preferences[n].Name

Upvotes: 4

Views: 9830

Answers (4)

Manek
Manek

Reputation: 568

I suggest using a Lambda foreach

preferences.ForEach(preference =>
                {
                    var sap = sapReadOnlyItems.FirstOrDefault(s => s.ItemValue = preference.ItemValue);
                    preference.Name = (sap != null) ? sap.Name : string.Empty;
                });

Upvotes: 7

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

You can do join and assign name with standard linq operator:

var query = preferences.Join(sapReadOnlyItems, 
                             p => p.ItemValue, 
                             s => s.ItemValue, 
                             (p, s) => { p.Name = s.Name; return p; });

Upvotes: 5

Mike Marynowski
Mike Marynowski

Reputation: 3439

I don't believe that what you want can be done in the special c# syntax, rather you would need to use the regular method chaining syntax. Two options:

Method #1:

Here is a foreach extension method if you want to use it with Linq syntax:

sapReadOnlyItems.ForEach(s => preferences.Single(p => p.ItemValue == s.ItemValue).Name = s.Name);

Used with this:

public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
{
    foreach(T item in items)
    {
        action(item);
    }
}

Method #2:

If you want the updated preferences back, you can do this:

var updatedPreferences = sapReadOnlyItems.Select(s => 
                         { 
                             var p = preferences.Single(p => p.ItemValue == s.ItemValue);
                             p.Name = s.Name;
                             return p;
                         });

Upvotes: 0

dogeter
dogeter

Reputation: 41

I came up with this:

foreach (Preference pref in preferences)
{
    pref.Name = sapReadOnlyItems.Where(sapItem => sapItem.ItemValue == pref.ItemValue).FirstOrDefault().Name;
}

Upvotes: 0

Related Questions