简体   繁体   中英

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 SupplierSettingsPropertyEnum 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"});

are you looking for something as below

var SupplierSettingsVales = SupplierSettings.
Where(x=>x.property==SupplierSettingsPropertyEnum.Interval)
    .Select(x=>x.Value);

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM