[英]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
給定匹配項讀取CollectionNameID
和CollectionName
。
這是我為CollectionName
嘗試的方式:
string CollectionName = ButterFlyList.Where(x => x.CollectionName == butterflyName)
但是我需要CollectionNameID
和CollectionName
,該怎么辦?
這就是我要的 :
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.