我有2個對象列表。一個有價值觀,另一個有名字和價值觀。我想查看其他列表中的值並將結果寫入pref.Name。我知道我可以用foreach做到這一點,但我認爲在LINQ中有很好的方法。想法?如何使用LINQ從另一個列表填充對象的屬性
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
foreach所IEnumerable的... http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx http://blogs.msdn.com/b/馬洲/存檔/ 2011/09/21 /爲什麼-NO-的foreach方法上,IEnumerable的-interfac es.aspx – Maarten