繁体   English   中英

如何在C#中使用linq读取列表中的列表项?

[英]How read list items in list using linq in C#?

我有以下课程

[Serializable]
[DataContract]
public class R_ButterFly_Collection
{
        [DataMember]
        public Int64? CollectionNameID { get; set; }
        [DataMember]
        public string CollectionName { get; set; }
}

我需要为collection_name给定匹配项读取CollectionNameIDCollectionName

这是我为CollectionName尝试的方式:

string CollectionName = ButterFlyList.Where(x => x.CollectionName == butterflyName)

但是我需要CollectionNameIDCollectionName ,该怎么办?

这就是我要的 :

Int64 CollectionNameID, CollectionName = ButterFlyList.Where(x => x.CollectionName == butterflyName)

假设:

var ButterFlyList = new List<R_ButterFly_Collection>()
            {
                new R_ButterFly_Collection()
                {
                    CollectionName="one",
                    CollectionNameID=1
                },
                new R_ButterFly_Collection()
                {
                    CollectionName="two",
                    CollectionNameID=2
                },
                new R_ButterFly_Collection()
                {
                    CollectionName="three",
                    CollectionNameID=3
                }
            };

然后,您可以使用linq并返回元组结果:

var results = ButterFlyList.Where(x => x.CollectionName == "one").Select(p => new Tuple<long, string>(p.CollectionNameID ?? 0, p.CollectionName));

请注意,您需要为CollectionNameID字段处理可为空的long。

您可以使用 :

 List<R_ButterFly_Collection> results = ButterFlyList.Where(x => x.CollectionName == butterflyName).ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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