简体   繁体   English

如何使用C#在LINQ中使用。

[英]How to use .Contains in LINQ using C#

I am trying to find a substring of states in a file. 我正在尝试在文件中找到状态的子字符串。 I open the file and load each line a string at a time. 我打开文件并一次在每一行中加载一个字符串。 I would then like to check if each string contains one of the states in my substring. 然后,我想检查每个字符串是否包含我的子字符串中的状态之一。 It is not working as intended as it keeps returning "Could not find substring" even though I know that the states are in the string. 即使我知道状态在字符串中,它也不会按预期方式工作,因为它一直返回“找不到子字符串”。 What am I doing wrong? 我究竟做错了什么?

EDIT: I realise now what the error, this line was completely wrong: 编辑:我现在知道什么错误,此行是完全错误的:

if (lines.Any(stringToCheck.Contains))

It should be like this: 应该是这样的:

if (stringToCheck.Any(s.Contains))

Thanks for the help guys. 感谢您的帮助。

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

namespace ConsoleApplication2
{
    class Program
    {
       static void Main (string[] args)
    {

        string[] stringToCheck = {"Alabama","Alaska","Arizona","Arkansas","California","Colorado"}; 

        string[] lines = File.ReadAllLines(@"C:\C# Project\sampledata.dat");
        foreach (string s in lines)
        {
            if (lines.Any(stringToCheck.Contains))
            {
                Console.WriteLine("Found substring");
                Console.WriteLine(s);
                Console.ReadLine();
            }
            else
            Console.WriteLine("Could not find substring");
            Console.WriteLine(s);
            Console.ReadLine() ;
        }


    }
}

} }

You could use Any over the list of states to check if there is any string for each state which contains on the line. 您可以在状态列表中使用Any来检查行中包含的每个状态是否存在任何字符串。 For sample: 样品:

    if (stringToCheck.Any(x = > s.Contains(x))
    {
      // ...
    }

you could do something like this: 您可以执行以下操作:

if(stringToCheck.Any(e => s.Contains(e)){
   //
}

You could do something like this: 您可以执行以下操作:

string[] stringToCheck = { "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado" };

//Some test lines
string[] lines = { "sadnaskjd Alabama", "sadasd Arizona", "asdasdaer" };

//A bool telling me if I found anything, 
//you could skip the bool and just use else in the foreach :)
bool contains = false;


foreach ( var check in stringToCheck )
{
    if ( lines.Any( l => l.Contains( check ) ) )
    {
        Console.WriteLine( "Found substring" );
        Console.WriteLine( s );
        contains = true;
    }  
}

if ( contains == false )
{
    Console.WriteLine("Could not find substring");
    Console.ReadLine();
}

Try this idea: 试试这个想法:

if (line.Any(x=>CheckKeyWordsInLine(line, stringToCheck)){
     //The line contains one of the keywords
}else{
     //The line does not contain any of the keywords
}

Then define the CheckKeyWordsInLine function as: 然后将CheckKeyWordsInLine函数定义为:

bool CheckKeyWordsInLine(string line, string[] keywords){

 foreach(var key in keywords)
 {
   if(line.Contains(key))
   {
      return true;
   }
 }
 return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM