Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 679059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:13:05+00:00 2026-05-14T01:13:05+00:00

the ToSelectList method I have: public static IList<SelectListItem> ToSelectList<T>(this IEnumerable<T> itemsToMap, Func<T, string> textProperty,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:13:05+00:00Added an answer on May 14, 2026 at 1:13 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an extension method that I'm trying to use where you can use
Say I have an enum something like: enum OrderStatus { AwaitingAuthorization, InProduction, AwaitingDespatch }
So I got this crazy idea I could make make something cool work. I
This question relates to ASP.NET MVC 2 RC (December drop). The basic problem is
I have a dropdown box which only shows active items in the list. But
I got a model defined as IEnumerable<MyViewModel> which I tried to use to create
I need an extention for converting ToSelectList a list of items (strings or ints)
Hey, I Have three IQueryable lists which i concat together into one list to
I wrote a helper method to display enums from my model in my asp.net
I create a dropdownlist from Enum. public enum Level { Beginner = 1, Intermediate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.