我在这一行收到InvalidCastException:
List<Student> studentsWithSameSurname =
(List<Student>)_studentsList
.Where(i => i.LastName == textBoxLastName.Text.Trim())
.GroupBy(j => j.FamilyID);
_studentsList
是List<Student>
,所以我不知道为什么(List<Student>
)是无效的演员。
对于那些想要更多上下文的人来说,这是一个Winforms应用程序,其中我将自定义类的通用列表序列化为json文件,然后在我想处理数据时反序列化它,并使用LINQ查询它。
_studentsList
以这种方式定义:
List<Student> _studentsList;
学生班是:
public class Student
{
public int StudentID { get; set; }
public int FamilyID { get; set; }
public bool EnrolledInAYttFM { get; set; }
public DateTime DateEnrolledOrHiatusAYttFM { get; set; }
public bool GivesBibleReading { get; set; }
public bool PresentsICRVBS { get; set; }
public bool IsHouseholder { get; set; }
public bool IsMale { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddr { get; set; }
public DateTime WeekOfLastAssignment { get; set; }
public int RecommendedNextTalkTypeID { get; set; }
public int NextCounselPoint { get; set; }
}
我正在努力做的事情背后的逻辑是:
如果添加了学生,并且他们的姓氏已经存在于通用列表中,则需要确定该名称的第二个实例是否是与前一个实例相同的成员。更具体地说,如果添加“史密斯”,并且他们是第一个具有该姓氏的人,他们将获得下一个可用的familyID;否则(如果已经输入该姓氏)必须确定他们是否属于已经进入的其中一个家庭,或者他们是否是新家庭的第一个成员:
Student surNameCandidateRecord = _studentsList.SingleOrDefault(s => s.LastName == textBoxLastName.Text);
if (null == surNameCandidateRecord)
{
student.FamilyID = _studentsList.Max(x => x.FamilyID) + 1;
}
else // at least one family with that surname exists already
{
student.FamilyID = GetFamilyID();
}
GetFamilyID()方法是抛出此异常的方法,在这里它是完整的:
private int GetFamilyID()
{
List<KeyValuePair<int, string>> familyIDsAndNames;
// InvalidCastException on the line below
List<Student> studentsWithSameSurname = (List<Student>)_studentsList.Where(i => i.LastName == textBoxLastName.Text.Trim()).GroupBy(j => j.FamilyID);
// </ InvalidCastException on the line above
familyIDsAndNames = new List<KeyValuePair<int, string>>();
foreach (Student s in studentsWithSameSurname)
{
string fullNameWithFamilyIdPrepended = String.Format("({0}). {1} {2}", s.FamilyID, s.FirstName, s.LastName);
familyIDsAndNames.Add(new KeyValuePair<int, string>(s.FamilyID, fullNameWithFamilyIdPrepended));
}
ChooseFamilyForm cff = new ChooseFamilyForm(familyIDsAndNames);
cff.ShowDialog();
return AYttFMConstsAndUtils.familyIDSelected;
}
在抛出InvalidCastException的时候,通用列表的内容是(名称和电子邮件地址已被更改以保护垃圾邮件):
[{"StudentID":1,"FamilyID":1,"EnrolledInAYttFM":true,"DateEnrolledOrHiatusAYttFM":"2016-02-12T21:52:55.9560088-08:00","GivesBibleReading":false,"PresentsICRVBS":true,"IsHouseholder":true,"IsMale":false,"FirstName":"Carly","LastName":"Shannon","EmailAddr":"carlyjeannette@att.net","WeekOfLastAssignment":"2015-08-12T21:52:50.6959113-07:00","RecommendedNextTalkTypeID":3,"NextCounselPoint":16},{"StudentID":2,"FamilyID":2,"EnrolledInAYttFM":true,"DateEnrolledOrHiatusAYttFM":"0001-01-01T00:00:00","GivesBibleReading":false,"PresentsICRVBS":true,"IsHouseholder":true,"IsMale":false,"FirstName":"Jennifer","LastName":"Volando","EmailAddr":"jenni_v@ymail.com","WeekOfLastAssignment":"0001-01-01T00:00:00","RecommendedNextTalkTypeID":0,"NextCounselPoint":0}]
...我正在尝试添加第二个姓“Volando”的人(因此将代码路由到GetFamilyID()方法)。
我的LINQ查询可能有误,但我不知道以哪种方式。
这是我成功使用的,基于Tordek的回答:
List<Student> studentsWithSameSurname = (List<Student>)_studentsList
.Where(i => i.LastName == textBoxLastName.Text.Trim())
.OrderBy(j => j.FamilyID)
.ToList();
我将尽快给予dicho奖励;自从达到10K后,我将做一个卡内基并且“放弃”所有点。
答案 0 :(得分:9)
在这一行
List<Student> studentsWithSameSurname =
(List<Student>)_studentsList
.Where(i => i.LastName == textBoxLastName.Text.Trim())
.GroupBy(j => j.FamilyID);
你没有施放_studentsList
;你正在施展整个表达。换句话说,这相当于
(List<Student>)(_studentsList
.Where(i => i.LastName == textBoxLastName.Text.Trim())
.GroupBy(j => j.FamilyID));
而不是(如你所料)
((List<Student>)_studentsList)
.Where(i => i.LastName == textBoxLastName.Text.Trim())
.GroupBy(j => j.FamilyID);
如果要获取Linq查询的结果列表,则必须使用.ToList()方法:
studentsList
.Where(i => i.LastName == textBoxLastName.Text.Trim())
.GroupBy(j => j.FamilyID)
.ToList();
但是,您的结果不是List<Student>
,而是新类型,因为GroupBy
方法会创建一个新的学生列表类型。 GroupBy
表达式为IEnumerable<IGrouping<T, U>>
的os的结果,其中T是您的分组变量,U是元素&#39;类型;在您的情况下,如果IEnumerable<IGrouping<int, Student>>
的类型为FamilyID
,则为int
。