ToSelectList 中的谓词问题
我有的 ToSelectList
方法:
public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> itemsToMap, Func<T, string> textProperty, Func<T, string> valueProperty, Predicate<T> isSelected)
{
var result = new List<SelectListItem>();
foreach (var item in itemsToMap)
{
result.Add(new SelectListItem
{
Value = valueProperty(item),
Text = textProperty(item),
Selected = isSelected(item)
});
}
return result;
}
当我在这里调用此方法时:
public static List<SelectListItem> lesgeverList(int selectedID) {
NASDataContext _db = new NASDataContext();
var lesg = (from l in _db.Lesgevers
where l.LG_Naam != "leeg"
orderby l.LG_Naam
select l).ToSelectList(m => m.LG_Naam + " " + m.LG_Vnaam, m => m.LG_ID.ToString(), m => m.LG_ID == selectedID);
return lesg.ToList();
}
我得到的 List
已选择 selectedID
。
现在,当我想要有多个选定的项目时,我给出一个 Lesgevers 列表,
public static List<SelectListItem> lesgeverList(List<Lesgever> lg) {
NASDataContext _db = new NASDataContext();
var test = (from l in _db.Lesgevers
where l.LG_Naam != "leeg" && lg.Contains(l)
orderby l.LG_Naam, l.LG_Vnaam
select l).ToList();
var lesg = (from l in _db.Lesgevers
where l.LG_Naam != "leeg"
orderby l.LG_Naam, l.LG_Vnaam
select l).ToSelectList(m => m.LG_Naam + " " + m.LG_Vnaam, m => m.LG_ID.ToString(), m => lg.Contains(m));
return lesg.ToList();
}
var test
确实返回我在 lg 列表中的 Lesgevers
,在我的 var lesg
,根本没有选择任何 selectlistitem。
我的错误在哪里? :) 我该如何解决这个问题?
the ToSelectList
method I have:
public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> itemsToMap, Func<T, string> textProperty, Func<T, string> valueProperty, Predicate<T> isSelected)
{
var result = new List<SelectListItem>();
foreach (var item in itemsToMap)
{
result.Add(new SelectListItem
{
Value = valueProperty(item),
Text = textProperty(item),
Selected = isSelected(item)
});
}
return result;
}
when I call this method here:
public static List<SelectListItem> lesgeverList(int selectedID) {
NASDataContext _db = new NASDataContext();
var lesg = (from l in _db.Lesgevers
where l.LG_Naam != "leeg"
orderby l.LG_Naam
select l).ToSelectList(m => m.LG_Naam + " " + m.LG_Vnaam, m => m.LG_ID.ToString(), m => m.LG_ID == selectedID);
return lesg.ToList();
}
the List<SelectListItem>
I get has the selectedID
as selected.
now, when I want to have multiple selected items, I give a list of Lesgevers
public static List<SelectListItem> lesgeverList(List<Lesgever> lg) {
NASDataContext _db = new NASDataContext();
var test = (from l in _db.Lesgevers
where l.LG_Naam != "leeg" && lg.Contains(l)
orderby l.LG_Naam, l.LG_Vnaam
select l).ToList();
var lesg = (from l in _db.Lesgevers
where l.LG_Naam != "leeg"
orderby l.LG_Naam, l.LG_Vnaam
select l).ToSelectList(m => m.LG_Naam + " " + m.LG_Vnaam, m => m.LG_ID.ToString(), m => lg.Contains(m));
return lesg.ToList();
}
the var test
does return the Lesgevers
that i have in the lg List, in my var lesg
, there are no selectlistitem's selected at all.
where is my mistake? :) how do I fix thix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设 NASDataContext 是 Linq-to-SQL 或 Linq-to-Entities 数据上下文。
在
var test
选择中,lg.Contains(l)
作为 linq 表达式的一部分进行计算,这意味着它将被转换为 SQL 并在 SQL Server 上执行。这让 SQL Server 来确定 contains 语句的相等性。它可能会使用Lesgever
表的主键转换为WHERE IN (...)
子句。在您的
ToSelectList
方法中,您在isSelected
谓词中使用对象相等性。列表中的项目将是数据上下文创建的全新对象。即使它们与列表lg
中的对象对应相同的实体,它们也不会是相同的对象。尝试调整你的谓词
m => lg.Contains(m)
比较项目的键,而不是项目对象本身。或者,您可以实现IEquatable
接口,使isSelected
使用您自己的相等定义。I assume that
NASDataContext
is a Linq-to-SQL or Linq-to-Entities data context.In the
var test
selection,lg.Contains(l)
is evaluated as part of the linq expressions, which means it will be translated into SQL and executed on the SQL server. That leaves it to the SQL server to determine equality for the contains statement. It will probably be converted to aWHERE IN (...)
clause using the primary key of theLesgever
table.In your
ToSelectList
method you are instead using object equality in theisSelected
predicate. The items in the list will be fresh new objectes created by the data context. Even if they correspond to the same entity as the objects in the listlg
, they will not be the same objects.Try to adjust your predicate
m => lg.Contains(m)
to compare on the keys of the item, instead of the item object itself. Alternatively you can implement theIEquatable<T>
interface to makeisSelected
use your own equality definition.