ToSelectList 中的谓词问题

发布于 2024-08-25 18:22:04 字数 2026 浏览 11 评论 0原文

我有的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ら栖息 2024-09-01 18:22:04

我假设 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 a WHERE IN (...) clause using the primary key of the Lesgever table.

In your ToSelectList method you are instead using object equality in the isSelected 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 list lg, 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 the IEquatable<T> interface to make isSelected use your own equality definition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文