Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

c# - find if an integer exists in a list of integers

i have this code:

  List<T> apps = getApps();

        List<int> ids;

        List<SelectListItem> dropdown = apps.ConvertAll(c => new SelectListItem
        {
            Selected = ids.Contains(c.Id),
            Text = c.Name,
            Value = c.Id.ToString()
        }).ToList();


ids.Contains

seems to always return false even though the numbers do match

any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you just need a true/false result

bool isInList = intList.IndexOf(intVariable) != -1;

if the intVariable does not exist in the List it will return -1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...