2013-03-15 85 views
1

我創建了一個結構:如何從列表<struct>項目的組件創建列表<Guid>?

public struct ProductShortInfo 
      { 
       public Guid Id { get; set; } 

       public string Code { get; set; } 

       public string Name { get; set; } 

       public string PhotoUrl { get; set; } 

       public string DownloadUrl { get; set; } 
      } 

public string PopulateStruct() 
{ 
    List<ProductShortInfo> productShortData; 

    using (SomeService service = new SomeService()) 
    { 
     productShortData = service.populateShortData(); 
    } 

    List<Guid> guidsFromProductShortData = //NEED SOLUTION 
} 

我想孤立ID,用於以後在列表中使用返回,但我想不通的正確方法。我真的不認爲一個foreach循環是一個非常優雅的方式。 Theres可能是一種內置的方法。

編輯:找到一個解決方案少於5分鐘後。 LINQ。

列表productShortDataIds =(來自productShortData中的數據select datas.Id).ToList();

回答

5

你可以結構的project名單與LINQ:

List<Guid> guids = productShortData.Select(p => p.Id).ToList();