查找整数列表中是否存在整数

发布于 2024-09-27 10:38:53 字数 392 浏览 3 评论 0原文

我有这个代码:

  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

即使数字确实符合

任何想法,似​​乎总是返回 false?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

奶茶白久 2024-10-04 10:38:53

如果您只需要 true/false 结果,

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

如果列表中不存在 intVariable 它将返回 -1

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

爱的故事 2024-10-04 10:38:53

只要您的列表是用值初始化的,并且该值实际上存在于列表中,那么 Contains 就应该返回 true。

我尝试了以下操作:

var list = new List<int> {1,2,3,4,5};
var intVar = 4;
var exists = list.Contains(intVar);

并且存在确实设置为true。

As long as your list is initialized with values and that value actually exists in the list, then Contains should return true.

I tried the following:

var list = new List<int> {1,2,3,4,5};
var intVar = 4;
var exists = list.Contains(intVar);

And exists is indeed set to true.

坏尐絯℡ 2024-10-04 10:38:53

这是一个扩展方法,它允许像 SQL IN 命令一样进行编码。

public static bool In<T>(this T o, params T[] values)
{
    if (values == null) return false;

    return values.Contains(o);
}
public static bool In<T>(this T o, IEnumerable<T> values)
{
    if (values == null) return false;

    return values.Contains(o);
}

这允许这样的事情:

List<int> ints = new List<int>( new[] {1,5,7});
int i = 5;
bool isIn = i.In(ints);

或者:

int i = 5;
bool isIn = i.In(1,2,3,4,5);

Here is a extension method, this allows coding like the SQL IN command.

public static bool In<T>(this T o, params T[] values)
{
    if (values == null) return false;

    return values.Contains(o);
}
public static bool In<T>(this T o, IEnumerable<T> values)
{
    if (values == null) return false;

    return values.Contains(o);
}

This allows stuff like that:

List<int> ints = new List<int>( new[] {1,5,7});
int i = 5;
bool isIn = i.In(ints);

Or:

int i = 5;
bool isIn = i.In(1,2,3,4,5);
夏有森光若流苏 2024-10-04 10:38:53

你的做法是正确的。它与该代码配合良好: x 为 true。
可能你在其他地方犯了错误。

List<int> ints = new List<int>( new[] {1,5,7}); // 1

List<int> intlist=new List<int>() { 0,2,3,4,1}; // 2

var i = 5;
var x = ints.Contains(i);   // return true or false

The way you did is correct. It works fine with that code: x is true.
probably you made a mistake somewhere else.

List<int> ints = new List<int>( new[] {1,5,7}); // 1

List<int> intlist=new List<int>() { 0,2,3,4,1}; // 2

var i = 5;
var x = ints.Contains(i);   // return true or false
要走干脆点 2024-10-04 10:38:53

最好的完整代码在这里:

NumbersList.Exists(p => p.Equals(Input)

使用:

List<int> NumbersList = new List<int>();
private void button1_Click(object sender, EventArgs e)
{
    int Input = Convert.ToInt32(textBox1.Text);
    if (!NumbersList.Exists(p => p.Equals(Input)))
    {
       NumbersList.Add(Input);
    }
    else
    {
        MessageBox.Show("The number entered is in the list","Error");
    }
}

The best of code and complete is here:

NumbersList.Exists(p => p.Equals(Input)

Use:

List<int> NumbersList = new List<int>();
private void button1_Click(object sender, EventArgs e)
{
    int Input = Convert.ToInt32(textBox1.Text);
    if (!NumbersList.Exists(p => p.Equals(Input)))
    {
       NumbersList.Add(Input);
    }
    else
    {
        MessageBox.Show("The number entered is in the list","Error");
    }
}
我的影子我的梦 2024-10-04 10:38:53
bool vExist = false;
int vSelectValue = 1;

List<int> vList = new List<int>();
vList.Add(1);
vList.Add(2);

IEnumerable vRes = (from n in vListwhere n == vSelectValue);
if (vRes.Count > 0) {
    vExist = true;
}
bool vExist = false;
int vSelectValue = 1;

List<int> vList = new List<int>();
vList.Add(1);
vList.Add(2);

IEnumerable vRes = (from n in vListwhere n == vSelectValue);
if (vRes.Count > 0) {
    vExist = true;
}
只有一腔孤勇 2024-10-04 10:38:53

您应该引用 Selected 而不是 ids.Contains 作为最后一行。

我刚刚意识到这是来自OP的格式问题。无论如何,您应该引用 Selected 中的值。我建议添加一些 Console.WriteLine 调用,以准确查看每行打印的内容以及每个值是什么。

更新后:
ids 是一个空列表,这怎么会不抛出 NullReferenceException 呢?因为它从未在该代码块中初始化

You should be referencing Selected not ids.Contains as the last line.

I just realized this is a formatting issue, from the OP. Regardless you should be referencing the value in Selected. I recommend adding some Console.WriteLine calls to see exactly what is being printed out on each line and also what each value is.

After your update:
ids is an empty list, how is this not throwing a NullReferenceException? As it was never initialized in that code block

梦罢 2024-10-04 10:38:53
string name= "abc";
IList<string> strList = new List<string>() { "abc",  "def", "ghi", "jkl", "mno" };
if (strList.Contains(name))
{
  Console.WriteLine("Got It");
}

/////////////////   OR ////////////////////////

IList<int> num = new List<int>();
num.Add(10);
num.Add(20);
num.Add(30);
num.Add(40);

Console.WriteLine(num.Count);   // to count the total numbers in the list

if(num.Contains(20)) {
    Console.WriteLine("Got It");    // if condition to find the number from list
}
string name= "abc";
IList<string> strList = new List<string>() { "abc",  "def", "ghi", "jkl", "mno" };
if (strList.Contains(name))
{
  Console.WriteLine("Got It");
}

/////////////////   OR ////////////////////////

IList<int> num = new List<int>();
num.Add(10);
num.Add(20);
num.Add(30);
num.Add(40);

Console.WriteLine(num.Count);   // to count the total numbers in the list

if(num.Contains(20)) {
    Console.WriteLine("Got It");    // if condition to find the number from list
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文