Predicate problem in ToSelectList

Posted by Stefanvds on Stack Overflow See other posts from Stack Overflow or by Stefanvds
Published on 2010-03-18T08:18:29Z Indexed on 2010/03/18 8:21 UTC
Read the original article Hit count: 568

Filed under:
|
|

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 selectlist 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?

© Stack Overflow or respective owner

Related posts about predicate

Related posts about linq-to-sql