从另一个列表中过滤列表对象

发布于 2024-07-13 05:38:04 字数 829 浏览 10 评论 0原文

我的 C# .NET 3.5 win 表单应用程序中有以下类:

class Field {

string objectName;
string objectType;
string fieldName;
string fieldValue;


}

和一个 List fieldList,它是 checklistbox 的数据源。 该列表框显示了我的 fieldList 集合中所有不同的 objectName。

我想创建另一个包含 fieldNames 的选中列表框,但仅显示在第一个列表框中具有关联的选中 objectName 的字段名。

所以我的问题是如何查询原始 objectName 列表的 DataSource 以返回与所选 objectName 关联的不同 fieldName 集?

这不太容易阅读,所以我将举一个例子:

Field1 {

objectName = 'objA'
FieldName = 'FieldA'

}

Field2 {

objectName = 'objA'
FieldName = 'FieldB'

}


Field3 {

objectName = 'objB'
FieldName = 'FieldA'

}

Field4 {

objectName = 'objC'
FieldName = 'FieldC'

}

所以假设在我的复选框中我选择 objectNames objA 和 objB。 那么我返回的字段将是“FieldA”和“FieldB”。

如何使用 LINQ 或过滤通用字段列表来实现此目的? 我可以使用列表可用的“选择”或“位置”方法吗?

I have the following class in my C# .NET 3.5 win forms app:

class Field {

string objectName;
string objectType;
string fieldName;
string fieldValue;


}

and a List fieldList that is a datasource for a checkedlistbox. This listbox shows all the distinct objectNames from my fieldList collection.

I want to create another checkedlistbox that contains fieldNames, but only shows fieldnames that have an associated checked objectName in the first list box.

So my question is how can I query the DataSource of the original list of objectNames to return the distinct set of fieldNames that are associated with a selected objectName?

That is not very easy to read so I will give an example:

Field1 {

objectName = 'objA'
FieldName = 'FieldA'

}

Field2 {

objectName = 'objA'
FieldName = 'FieldB'

}


Field3 {

objectName = 'objB'
FieldName = 'FieldA'

}

Field4 {

objectName = 'objC'
FieldName = 'FieldC'

}

So suppose in my checkbox I select objectNames objA and objB. Then my returned fields would be 'FieldA' and 'FieldB'.

How can I achieve this using LINQ or filtering my generic list of Fields? Can I utilise the 'select' or 'where' methods that are available to a list?

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

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

发布评论

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

评论(2

梦里°也失望 2024-07-20 05:38:04

首先,将对象名称读入数组或列表; 我会伪造那部分。 那么它应该是这样的:(

    string[] objectNames = { "objA", "objC" };
    var hashSet = new HashSet<string>(objectNames);

    var qry = (from row in data
               where hashSet.Contains(row.objectName)
               select row.fieldName).Distinct().ToList();

编辑)

要获取选定的名称(我伪造的位),您可以尝试(未经测试):(

    var selectedNames = namesCheckedListBox.CheckedItems.Cast<Field>()
        .Select(field => field.objectName);
    var hashSet = new HashSet<string>(selectedNames);

注意上面不需要使用 Distinct() ,因为 < code>HashSet无论如何都会这样做)

First, read the object names into an array or list; I'll fake that part. Then it should be something like:

    string[] objectNames = { "objA", "objC" };
    var hashSet = new HashSet<string>(objectNames);

    var qry = (from row in data
               where hashSet.Contains(row.objectName)
               select row.fieldName).Distinct().ToList();

(edit)

To get the selected names (the bit I faked) you could try (untested):

    var selectedNames = namesCheckedListBox.CheckedItems.Cast<Field>()
        .Select(field => field.objectName);
    var hashSet = new HashSet<string>(selectedNames);

(note no need to use Distinct() in the above, since HashSet<T> does that anyway)

无力看清 2024-07-20 05:38:04
var selectedNames = ... // List of selected names
var selectedFields = (from f in fieldList
                      where selectedNames.Contains(f.objectName)
                      select f.FieldName).Distinct().ToList();
var selectedNames = ... // List of selected names
var selectedFields = (from f in fieldList
                      where selectedNames.Contains(f.objectName)
                      select f.FieldName).Distinct().ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文