我在SortedList上使用Linq时遇到了麻烦。我有一个类库和一个Test WinForms应用程序。类库代码是:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JTS
{
public static class Pictures
{
public static class PictureManagement
{
public static SortedList<int, Picture> PictureList = new SortedList<int, Picture>();
public static class Acquire
{
public static Picture ById(int id)
{
Picture picture = null;
if(PictureList != null)
{
picture = (Picture)from pic in PictureList
where
pic.Key == id
select (pic.Value as Picture);
}
return picture;
}
}
}
public class Picture : PictureBox
{
public int Id { get; set; }
public class PictureProperties
{
public string Name { get; set; }
public string Extension { get; set; }
public string OriginalPath { get; set; }
public Image OriginalPhoto { get; set; }
public string Credits { get; set; }
public Size DesignTimeSize { get; set; }
public Point DesignTimeLocation { get; set; }
}
public PictureProperties Properties { get; set; }
public class PictureThumbnail
{
public string Name { get { return "T" + Name; } }
public Size Size { get; set; }
}
public PictureThumbnail Thumbnail { get; set; }
}
}
}
并且测试winforms应用代码是:
using JTS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void test()
{
for(int i = 0; i < 10; i++)
{
JTS.Pictures.PictureManagement.PictureList.Add(i, new JTS.Pictures.Picture()
{
Id = i,
Properties = new JTS.Pictures.Picture.PictureProperties()
{
Name = "Travis",
Credits = "people in here"
},
Thumbnail = new JTS.Pictures.Picture.PictureThumbnail()
{
Size = new Size(250, 250)
}
});
Console.WriteLine("pics added to list");
Console.WriteLine("getting Picture by id 3");
JTS.Pictures.Picture pic = JTS.Pictures.PictureManagement.Acquire.ById(3);
Console.WriteLine(pic.Id);
}
}
private void Form1_Load(object sender, EventArgs e)
{
test();
}
}
}
如果您要将其粘贴到VS中,请不要忘记添加System.Windows.Forms
作为类库的参考。
我遇到的问题是当我运行Test winforms应用程序时,在FormLoad上,我得到一个例外,其中说:
未处理的类型&#39; System.InvalidCastException&#39;发生了 在JTS.dll中
附加信息:无法投射类型的对象 &#39; {WhereSelectEnumerableIterator {1}} 2 [System.Int32,JTS.Pictures +照片],JTS.Pictures +照片]&#39; 输入图片&#39;。
现在,我知道什么是无效的演员阵容。但我不知道它指的是什么。我在哪里做了无效演员?
答案 0 :(得分:4)
根据错误消息,错误在于:
picture = (Picture)from pic in PictureList
where pic.Key == id
select (pic.Value as Picture);
试试这个:
picture = from pic in PictureList
where pic.Key == id
select (pic.Value as Picture)).FirstOrDefault();
或者,因为它是SortedList
,为什么不简单地使用它?
var picture = PictureList[id];
答案 1 :(得分:2)
picture = (Picture)from pic in PictureList
where
pic.Key == id
select (pic.Value as Picture);
此查询返回IEnumerable<Picture>
,而不是Picture
。
试试这个:
picture = (Picture)from pic in PictureList
where
pic.Key == id
select (pic.Value as Picture).First();
答案 2 :(得分:1)
此查询在执行后返回IEnumerable<T>
:
from pic in PictureList
where pic.Key == id
select (pic.Value as Picture)
如果您想获得单个项目,请使用Picture
或First
方法将其投放到单个Single
。
(from pic in PictureList
where pic.Key == id
select (pic.Value as Picture)).First();
答案 3 :(得分:1)
使用Lambda:
Picture picture = PictureList.Where(pic => pic.Key == id).FirstOrDefault().Value;
此代码替换您从PictureList集合中执行select的部分。您的代码的问题是where
子句返回Picture
的集合,并且您将集合分配给不是集合的Picture
,因此您获得了InvalidCastException
{1}}。