PramodChoudhari
PramodChoudhari

Reputation: 2553

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

Upvotes: 2

Views: 10402

Answers (4)

Pranay Rana
Pranay Rana

Reputation: 176886

are you looking for something as below

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

Upvotes: 2

Harsh Baid
Harsh Baid

Reputation: 7249

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

Upvotes: 2

Enigmativity
Enigmativity

Reputation: 117029

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.

Upvotes: 0

Viacheslav Smityukh
Viacheslav Smityukh

Reputation: 5833

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

Upvotes: 0

Related Questions