Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查条件语句中的字符串值列表_C#_List - Fatal编程技术网

C# 如何检查条件语句中的字符串值列表

C# 如何检查条件语句中的字符串值列表,c#,list,C#,List,我试图检查条件中字符串列表的值,而不创建for-each或counter循环 比如说 我想我可以使用一个.contains函数,它需要一个字符串,而不是字符串列表,所以它不会工作。您只需要翻转变量即可。您正在检查扩展名是否包含在ValidExtrade列表中: 你只需要翻转你的变量。您正在检查扩展名是否包含在ValidExtrade列表中: 这个怎么样 using System; using System.Collections.Generic; using System.Linq; using

我试图检查条件中字符串列表的值,而不创建for-each或counter循环

比如说


我想我可以使用一个.contains函数,它需要一个字符串,而不是字符串列表,所以它不会工作。

您只需要翻转变量即可。您正在检查扩展名是否包含在ValidExtrade列表中:


你只需要翻转你的变量。您正在检查扩展名是否包含在ValidExtrade列表中:

这个怎么样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace EasyE
{
class Program
{
    static void Main(string[] args)
    {
        // Test strings
        List<extention> extentions = new List<extention>() { 
            new extention { FileName = "File1.jif" }, 
            new extention() { FileName = "File2.TIFF" },
            new extention() { FileName = "File3.txt" } };
        ///////////////

        Regex regex = new Regex(@"^.*\.(tif|tiff|gif|jpeg|jif)$",RegexOptions.IgnoreCase);
        if ((from ext in extentions where (ext.FileName.ToString() == regex.Match(ext.FileName.ToString()).Value) select ext).Count() > 0)
        {

        }
    }
}

class extention
{
    public string FileName { get; set; }
}
}
这个怎么样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace EasyE
{
class Program
{
    static void Main(string[] args)
    {
        // Test strings
        List<extention> extentions = new List<extention>() { 
            new extention { FileName = "File1.jif" }, 
            new extention() { FileName = "File2.TIFF" },
            new extention() { FileName = "File3.txt" } };
        ///////////////

        Regex regex = new Regex(@"^.*\.(tif|tiff|gif|jpeg|jif)$",RegexOptions.IgnoreCase);
        if ((from ext in extentions where (ext.FileName.ToString() == regex.Match(ext.FileName.ToString()).Value) select ext).Count() > 0)
        {

        }
    }
}

class extention
{
    public string FileName { get; set; }
}
}

你应该考虑可变套管吗?可能是ValidExtensions。ContainesTention,StringComparer。CurrentCultureInogoreCase?@roryap-可能,但OP没有提到他们需要担心这个案子。如果他们这样做了,那么使用包含重载就很简单了@EasyE-对于小示例来说这很好,但是鉴于字符串是不可变的,不建议使用这种类型的字符串操作。我建议在比较字符串时使用大小写不变的选项,比如我的第一条注释中的重载,忘记转换成小写。只是未来的注意事项。你应该考虑可变套管吗?可能是ValidExtensions。ContainesTention,StringComparer。CurrentCultureInogoreCase?@roryap-可能,但OP没有提到他们需要担心这个案子。如果他们这样做了,那么使用包含重载就很简单了@EasyE-对于小示例来说这很好,但是鉴于字符串是不可变的,不建议使用这种类型的字符串操作。我建议在比较字符串时使用大小写不变的选项,比如我的第一条注释中的重载,忘记转换成小写。只是给未来的一个提示。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace EasyE
{
class Program
{
    static void Main(string[] args)
    {
        // Test strings
        List<extention> extentions = new List<extention>() { 
            new extention { FileName = "File1.jif" }, 
            new extention() { FileName = "File2.TIFF" },
            new extention() { FileName = "File3.txt" } };
        ///////////////

        Regex regex = new Regex(@"^.*\.(tif|tiff|gif|jpeg|jif)$",RegexOptions.IgnoreCase);
        if ((from ext in extentions where (ext.FileName.ToString() == regex.Match(ext.FileName.ToString()).Value) select ext).Count() > 0)
        {

        }
    }
}

class extention
{
    public string FileName { get; set; }
}
}