get property value from object collection list
I want to get value property from object collection using one of property of object collection.
using Linq what will be the query on SupplierSettingsList
public class SupplierSettings
{
private string Key;
private SupplierSettingsPr开发者_如何转开发opertyEnum property;
private string Value;
}
List<SupplierSettings> SupplierSettingsList =new List<SupplierSettingsDto>();
SupplierSettingsList .Add
(new SupplierSettings{Key="1",property=SupplierSettingsPropertyEnum.Name,Value="Name"});
SupplierSettingsList .Add
(new SupplierSettings{Key="2",property=SupplierSettingsPropertyEnum.StartTime,Value="7PM"});
SupplierSettingsList .Add
(new SupplierSettings{Key="3",property=SupplierSettingsPropertyEnum.EndTime,Value="10PM"});
SupplierSettingsList .Add
(new SupplierSettings{Key="4",property=SupplierSettingsPropertyEnum.Interval,Value="45"});
It can written as
var results = from o in SupplierSettingsList
where o.property == SupplierSettingsPropertyEnum.Interval
select o.Value;
Also you can find LINQ Query samples
in your C: drive
C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033
in that CSharpSamples.zip
unzip and build the project, located in folder LinqSamples
are you looking for something as below
var SupplierSettingsVales = SupplierSettings.
Where(x=>x.property==SupplierSettingsPropertyEnum.Interval)
.Select(x=>x.Value);
var value = SupplierSettings
.Where(x=>x.property==SupplierSettingsPropertyEnum.Interval)
.Select(x=>x.Value);
.FirstOrDefault();
Is this what you're trying to do:
var query =
from ss in SupplierSettingsList
where ss.property == SupplierSettingsPropertyEnum.Interval
select ss.Value;
I'm a little dubious about your SupplierSettings
as it doesn't seem to be a very good example of OOP. It may be better that you think about your object design rather than work out this query. Just a suggestion.
精彩评论