从对象集合列表中获取属性值

发布于 2024-10-19 11:08:33 字数 818 浏览 1 评论 0原文

我想使用对象集合的属性之一从对象集合中获取值属性。

使用 Linq 对 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"});

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄荷梦 2024-10-26 11:08:33

它可以写为“

var results = from o in SupplierSettingsList
              where o.property == SupplierSettingsPropertyEnum.Interval
              select o.Value;

您也可以在 C: 驱动器 中找到 LINQ 查询示例
C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033
CSharpSamples.zip 中解压缩并构建项目,位于文件夹 LinqSamples

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

染火枫林 2024-10-26 11:08:33

您在寻找以下内容吗

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

are you looking for something as below

var SupplierSettingsVales = SupplierSettings.
Where(x=>x.property==SupplierSettingsPropertyEnum.Interval)
    .Select(x=>x.Value);
神仙妹妹 2024-10-26 11:08:33
var value = SupplierSettings
  .Where(x=>x.property==SupplierSettingsPropertyEnum.Interval)
  .Select(x=>x.Value);
  .FirstOrDefault();
var value = SupplierSettings
  .Where(x=>x.property==SupplierSettingsPropertyEnum.Interval)
  .Select(x=>x.Value);
  .FirstOrDefault();
燕归巢 2024-10-26 11:08:33

这就是您想要做的吗:

var query =
    from ss in SupplierSettingsList
    where ss.property == SupplierSettingsPropertyEnum.Interval
    select ss.Value;

我对您的 SupplierSettings 有点怀疑,因为它似乎不是一个很好的 OOP 示例。您最好考虑对象设计而不是计算此查询。只是一个建议。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文