c# select one item from list code example
Example: select single item from list c#
using (var db = new ApplicationDbContext())
{
///itemModel is the model with gets & sets
var file = db.Table
.Where(x => x.Id == id)
.Select( x => new itemModel
{
itemTest = x.itemTest,
}).ToList();
///creating a new model
ModelList ML = new ModelList();
///creating a new list
ML.ViewModelList = new List<ViewModel>();
///getting each item from list
foreach(var item in file)
{
ViewModel img = new ViewModel();
img.itemTest = item.itemTest;
ML.ViewModelList.Add(img);
}
///selecting the last item from the list
var singleitem = ML.ViewModelList.Select(x => x.itemTest).LastOrDefault();
}