Here's my code that returns a list of articles and display them in a listbox. The strange thing is that it is working fine in Visual Studio 2019, but returns an error
Invalid cast
in Visual Studio 2017 !
The error happens at the line ArtListBox.Itemssource...
private void GetArticles()
{
DataClasses1DataContext dc = new DataClasses1DataContext();
IOrderedQueryable<FICHES_ARTICLES> art = (from a in dc.PESEES
join p in dc.FICHES_ARTICLES on a.ART_CODE equals p.ART_CODE
where a.PESEE_ST == 0
select p).Distinct().OrderBy(x => x.ART_LIBELLE1);
ArtListBox.ItemsSource = art;
ArtListBox.DisplayMemberPath = "ART_LIBELLE1";
ArtListBox.SelectedValuePath = "ART_CODE";
}
Can someone help me fix this error? I would really appreciate it.
答案 0 :(得分:1)
根据您的评论ArtListBox.ItemsSource
的类型为string
,因此您需要将字符串值分配给ItemsSource
属性
IOrderedQueryable<FICHES_ARTICLES> art = (from a in dc.PESEES
join p in dc.FICHES_ARTICLES on a.ART_CODE equals p.ART_CODE
where a.PESEE_ST == 0
select p).Distinct().OrderBy(x => x.ART_LIBELLE1);
ArtListBox.ItemsSource = art.FirstOrDefault();
FirstOrDefault()
将返回序列的第一个元素 分层a.PESEE_ST == 0
,如果找不到元素,则为默认值。