public void GetData(IList<long> lst) { MyConnection conn = new MyConnection (this.ConnectString); MyDataContext db = new MyDataContext (conn); var res=from al in db.MyTable where lst.Contains(al.ID) select al; // do something }
经过试验,将IList转换为数组后可以正常工作
public void GetData(IList<long> lst) { MyConnection conn = new MyConnection (this.ConnectString); MyDataContext db = new MyDataContext (conn); long[] ins =lst.ToArray(); var res=from al in db.MyTable where ins.Contains(al.ID) select al; // do something }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;
namespace 图片翻页 { public partial class WebForm1 : System.Web.UI.Page { public class mytable { public int ID { get; set; } public string name { get; set; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { List<mytable> list = new List<mytable>() { new mytable() { ID = 10, name = "ID1" }, new mytable() { ID = 20, name = "ID2" }, new mytable() { ID = 40, name = "ID4" }, new mytable() { ID = 30, name = "ID3" } }; IList<long> lst = new List<long>() { 10, 20,40 }; var res = from al in list where lst.Contains(al.ID) select al; res.ToList().ForEach(x => Response.Write(x.name)); } } } }
[解决办法]
[SerializableAttribute] [ComVisibleAttribute(true)] public abstract class Array : ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
数组也是继承自 IList的,所以不应该存在你的问题 [解决办法] 楼主 用的是 linq to sql ? linq to sql 的集查询方法好像是要有实现的,既具体的方法。 如:Contains 方法如果是 接口本身的Contains只是一个未实现方法声明 故无法转换。 如果是 使用具体的类的Contains,如 List Array 等都是可以的,
public void GetData(IList<long> lst) { MyConnection conn = new MyConnection (this.ConnectString); MyDataContext db = new MyDataContext (conn); var res=from al in db.MyTable where lst.ToArray().Contains(al.ID) select al; // do something }