Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从模型列表中获取最后一个值而不在MVC中循环?_C#_Linq_Asp.net Mvc 4 - Fatal编程技术网

C# 如何从模型列表中获取最后一个值而不在MVC中循环?

C# 如何从模型列表中获取最后一个值而不在MVC中循环?,c#,linq,asp.net-mvc-4,C#,Linq,Asp.net Mvc 4,在这里,我需要从视图中的模型列表中获取最后一个值,而不需要循环。这是我的控制器代码 public IList<ProductDetailModel> GetWPDetails() { ProductDetailModel Viewmodel; string funname = "GetCSpecialWPDetails"; List<ProductDetail

在这里,我需要从视图中的模型列表中获取最后一个值,而不需要循环。这是我的控制器代码

public IList<ProductDetailModel> GetWPDetails()
            {
                ProductDetailModel Viewmodel;
                string funname = "GetCSpecialWPDetails";
                List<ProductDetailModel> getWPDetails = new List<ProductDetailModel>();
                getWPDetails = objrest.EcommerceWPDetails(funname);
                List<ProductDetailModel> WPDetails = new List<ProductDetailModel>();

                foreach (var item in getWPDetails)
                {
                    Viewmodel = new ProductDetailModel();
                    Viewmodel.Productid = item.Productid;
                    Viewmodel.ProductName = item.ProductName;
                    Viewmodel.CategoryID = item.CategoryID;
                    Viewmodel.ProductRate = item.ProductRate;
                    Viewmodel.DiscountRate = item.DiscountRate;
                    Viewmodel.imageurl1 = item.imageurl1;
                    WPDetails.Add(Viewmodel);
                }
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
                SqlCommand cmd = new SqlCommand("SELECT ThemeColor,LayoutDesign FROM dbo.Cpecial_Partner_Design WHERE [PartnerID]='" + Partid + "'", con);
                con.Open();

                using (SqlDataReader myReader = cmd.ExecuteReader())
                {
                    while (myReader.Read())
                    {
                        Viewmodel = new ProductDetailModel();
                        Viewmodel.ThemeColor = myReader["ThemeColor"].ToString();
                        Viewmodel.LayoutDesign = myReader["LayoutDesign"].ToString();
                        WPDetails.Add(Viewmodel);
                    }

                    myReader.Close();
                }
                con.Close();
                return WPDetails;

            }

有什么建议吗?

您的模型似乎是IList,因此我建议您简单地使用以下方法:

var last;
if (Model != null && Model.Count > 0) last = Model[Model.Count - 1];
else
...
使用linq!在您的案例中,请使用方法

尝试:


我猜您的意思是“无需编写代码进行迭代”。=)您可以尝试
.Last()
。不要忘记检查null。如果您总是期望结果,那么最好使用
Last()
=)@Mateusz:当集合中没有项目时,这可能会导致
NullReferenceException
,因此您必须检查null,但是的,+1来自我,因为这是最简单的方法。如果不检查
null
,请不要使用
…或默认值
。改用普通扩展名(
第一个
单个
最后一个
)。
var last;
if (Model != null && Model.Count > 0) last = Model[Model.Count - 1];
else
...
@{ var last = Model.Last(); }
@Html.EditorFor(modelItem => last.ThemeColor)
@Html.EditorFor(modelItem => last.LayoutDesign)