问题描述
我是编程新手,有人可以帮我吗...
<div>
<h5>Total Number of Classes = @Model.Count(i => i.Class_ID)</h5>
</div>
如何解决这个.. 下图显示了错误错误 cs1662 和 cs0029
这是模型
// 命名空间 School_ManagementSystem.Models {
[Table("Tbl_Class")]
public class ClassModel
{
[Key]
[displayName("Class ID")]
public string Class_ID { get; set; }
[required(ErrorMessage = "Please Select a Lecturer")]
[displayName("Lecturer")]
public string Lecturer { get; set; }
[required(ErrorMessage = "Please Select the Start Date.")]
[displayName("Start Date")]
public DateTime Start_Date { get; set; }
[required(ErrorMessage = "Please Select the End Date.")]
[displayName("End Date")]
public DateTime End_Date { get; set; }
[required(ErrorMessage = "Please Provide a Valid Student Count")]
[displayName("Student Limit")]
public int Student_Count { get; set; }
[required(ErrorMessage = "Please Select a Status.")]
[displayName("Status")]
public string Status { get; set; }
[required(ErrorMessage = "Please Select a Course.")]
[displayName("Course")]
public string Course_ID { get; set; }
[required(ErrorMessage = "Please Select a Subject.")]
[displayName("Subject")]
public string Subject_ID { get; set; }
}
}
解决方法
Count LINQ 函数计算 lambda 计算结果为 TRUE 的行数。
// Count the number of records where the column is equal to 5
@Model.Count(x => x.SomeNumber == 5);
// Count the number of rows where some text contains some other text
@Model.Count(x => x.SomeText.Contains(“Something”));
错误告诉您,您的 lambda 不会计算为布尔值,因为 i.Class_ID 只是一个属性。
如果你想计算不同类的数量,你应该使用 CountDistinct:
@Model.CountDistinct(x => x.Class_ID);
如果你想不管重复的类的数量,你不应该传递一个lambda:
@Model.Count();