從模型列表中獲取模型

[英]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();

注意!

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



Get a list of items from a model get a list of a specific attribute from a list of model objects in a Django template Best way to get selected option in drop down list from model Django get base model from parent model Django model get duplicated from foreignkey model Django: Get list of model fields? Populate Array from List in Model DisplayNameFor() From List in Model How to get the Model from an ActionResult? Get list of occurrences + count in a model Django?
 
粤ICP备14056181号  © 2014-2020 ITdaan.com