[英]How read list items in list using linq in C#?
I have the following class 我有以下课程
[Serializable]
[DataContract]
public class R_ButterFly_Collection
{
[DataMember]
public Int64? CollectionNameID { get; set; }
[DataMember]
public string CollectionName { get; set; }
}
I need to read CollectionNameID
and CollectionName
for given match for collection_name
. 我需要为
collection_name
给定匹配项读取CollectionNameID
和CollectionName
。
This is how I tried for CollectionName
: 这是我为
CollectionName
尝试的方式:
string CollectionName = ButterFlyList.Where(x => x.CollectionName == butterflyName)
But I need CollectionNameID
and CollectionName
, how could I do that ? 但是我需要
CollectionNameID
和CollectionName
,该怎么办?
This is what I want : 这就是我要的 :
Int64 CollectionNameID, CollectionName = ButterFlyList.Where(x => x.CollectionName == butterflyName)
Assuming: 假设:
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
}
};
Then you can use linq and return tuple result: 然后,您可以使用linq并返回元组结果:
var results = ButterFlyList.Where(x => x.CollectionName == "one").Select(p => new Tuple<long, string>(p.CollectionNameID ?? 0, p.CollectionName));
Note that you need to handle nullable long for CollectionNameID field. 请注意,您需要为CollectionNameID字段处理可为空的long。
您可以使用 :
List<R_ButterFly_Collection> results = ButterFlyList.Where(x => x.CollectionName == butterflyName).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.