是否存在var.contains-C#

本文关键字:contains-C# var 存在 是否 | 更新日期: 2023-09-27 18:24:29

我对以下代码有一个问题:

private void Filter (object sender, Android.Text.TextChangedEventArgs e)
{
    List<Animal> animalList = new List<Animal>();
    if(!string.IsNullOrEmpty(_editText.Text)) 
    {
        foreach (string str in _animalList)
        {                
            if (str.Contains(_editText.Text))
            {
                animalList.Add (str);
            }
        }
    }
    _listView.Adapter = new AnimalAdapter(this, _animalList = animalList);
}

Animal类:

public class Animal
{
    private readonly int _intKey;
    public int AnimalNumber { get; private set; }
    public int StableNumber { get; private set; }
    public int LactoseNumber { get; private set; }
    public Animal ( int intKey, int animalNumber, int stableNumber, int lactoseNumber )
    {
        _intKey = intKey;
        AnimalNumber = animalNumber;
        StableNumber = stableNumber;
        LactoseNumber = lactoseNumber;
    }
    public override string ToString ()
    {
        return "Number: " + AnimalNumber + "'nGroup: " + StableNumber + "'nLactation: " + LactoseNumber;
    }
}

_animalList:声明

private List<Animal> _animalList;

我需要检查_animalList是否包含_editText.Text的输入。但是_animalList不是string,所以我需要使用var

有类似var.Contains的东西吗?还是我必须使用其他东西?

是否存在var.contains-C#

Contains方法可用于string类型。您需要将对象强制转换为string

A/c到您的类定义,您应该这样做:

foreach (Animal str in _animalList)
{                
    if (str.ToString().Contains(_editText.Text)) //using user defined "ToString()"
    {
        animalList.Add (str);
    }
}

您也可以检查个别属性:

foreach (Animal str in _animalList)
{                
    if (str.AnimalNumber.ToString().Contains(_editText.Text)) //if "AnimalNumber" is like "_editText.Text"
    {
        animalList.Add (str);
    }
}

与其尝试使用ToString进行筛选,不如使用实际属性值。例如:

var number = Convert.ToInt32(_editText.Text); 
var filteredList = _animalList
       .Where(x => x.AnimalNumber == number || 
                   x.StableNumber == number || 
                   x.LactoseNumber == number)
       .ToList();

否则,用户可以键入"Number",因为ToString覆盖包含该字符串,所以列表中的所有项目都将完全匹配。

(我在上面的代码中没有包含任何验证或错误检查,所以您也应该考虑这些)。

var inputText = _editText.Text;
int enteredNumber;
// you should make sure that the inputText is always an int
var isInt = int.TryParse(inputText, out enteredNumber);
//for example, if you are going to find by AnimalNumber, which is an int, you can use this. .
if (isInt){
    foreach (var animal in _animalList){
        var animalNumber = animal.AnimalNumber;
        if (animalNumber == enteredNumber)
        {
            animalList.Add(animal);
        }
    }
}

编辑(LINQ替代方案):

        if (isInt){
            animalList.AddRange(from animal in _animalList
                let animalNumber = animal.AnimalNumber
                where animalNumber == enteredNumber
                select animal);
        }
_animalList.Select(a => a.ToString()).Contains(_editText.Text)

如果任何动物对象的ToString方法的输出等于_editText.Text,则此表达式返回true

_animalList.Select(a => a.ToString()).Any(str => str.Contains(_editText.Text))

如果任何动物对象的ToString方法的输出包含_editText.Text(作为子字符串),则此表达式返回true。这相当于Shaharyar的回答。

var animalList = _animalList.Where(a => a.ToString().Equals(_editText.Text)).ToList();
var animalList = _animalList.Where(a => a.ToString().Contains(_editText.Text)).ToList();

这些语句直接过滤输入列表。