C# 如何创建列表<;Guid>;来自列表的组件<;结构>;项目?
我已经创建了一个结构:C# 如何创建列表<;Guid>;来自列表的组件<;结构>;项目?,c#,struct,C#,Struct,我已经创建了一个结构: public struct ProductShortInfo { public Guid Id { get; set; } public string Code { get; set; } public string Name { get; set; } public string PhotoUrl { get;
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
}
public struct ProductShortInfo
{
公共Guid Id{get;set;}
公共字符串代码{get;set;}
公共字符串名称{get;set;}
公共字符串PhotoUrl{get;set;}
公共字符串下载URL{get;set;}
}
公共字符串PopulateStruct()
{
列出数据;
使用(SomeService=newsomeservice())
{
productShortData=service.populateShortData();
}
List guidsFromProductShortData=//需要解决方案
}
我想隔离返回的Id,以便稍后在列表中使用,但我无法找到正确的方法。我真的不认为foreach循环是一种非常优雅的方式。可能有一个内置的方法来做这件事
编辑:在不到5分钟后找到解决方案。林克
List productShortDataId=(从productShortData中的数据选择datas.Id) 您可以使用LINQ列出结构:
List<Guid> guids = productShortData.Select(p => p.Id).ToList();
List guids=productShortData.Select(p=>p.Id.ToList();
您可以使用LINQ列出结构:
List<Guid> guids = productShortData.Select(p => p.Id).ToList();
List guids=productShortData.Select(p=>p.Id.ToList();