从模型列表中获取模型

[英]get model from List of a model


Is there a way to get the specific items from the list of a model without using a foreach of the list? like if you return a List<model> holding a model with a few differnt items to specifically call those item.

是否有一种方法可以在不使用列表foreach的情况下从模型列表中获取特定的项?例如,如果返回一个列表 ,其中包含一个模型,其中有一些不同的项可以专门调用这些项。

2 个解决方案

#1


1  

If you want a list of a specific item that's within your list then you can use LINQ to return those items, for example:

如果您想要列表中的某个特定项目,那么您可以使用LINQ来返回这些项,例如:

var customerNames = customersList.Select(customer => customer.Name);

This would give you a string collection with all of your customer names equal to the amount that was in your original list.

这将为您提供一个字符串集合,所有客户名与您原来列表中的数量相等。

Or if you would like to retrieve a number of different items from your model list then you may want to look into creating a class specifically for stripping out those items, for example:

或者,如果您想从模型列表中检索一些不同的项目,那么您可能想要创建一个专门用于剥离这些项目的类,例如:

public class CustomerContactDetails
{
    public string Address { get; set; }
    public string Email { get; set; }
    public string Telephone { get; set; }
}

...

var contactDetails = customersList.Select(customer => new CustomerContactDetails { Address = customer.Address, Email = customer.Email, Telephone = customer.Telephone });

#2


1  

You can use C# LambdaExpression, like this:

您可以使用c# LambdaExpression,如下所示:

var yourModel = modelList.Where(x => x.Id = 12).Select(x).First();
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/08/06/7202521289b71e9aa58b7a4e1334939b.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告