C# 使用Linq查找与条目匹配的行

C# 使用Linq查找与条目匹配的行,c#,linq,C#,Linq,我有以下格式的配置文件: keydemo1,this is a demo version of the software keyprod1,this is production version of the software 下面是基于键获取相关行的C#代码。因此,如果我通过:GetEntryFromConfigFile(“config.ini”,“keyprod1”),我希望下面的整行代码返回: "keyprod1, this is production version of the so

我有以下格式的配置文件:

keydemo1,this is a demo version of the software

keyprod1,this is production version of the software
下面是基于键获取相关行的C#代码。因此,如果我通过:
GetEntryFromConfigFile(“config.ini”,“keyprod1”)
,我希望下面的整行代码返回:

"keyprod1, this is production version of the software" 
但是,它不起作用。你能告诉我我做错了什么吗

public static string GetEntryFromConfigFile(string fileName, string entryToFind)
{
    //var m = File.ReadLines(fileName).Where(l => l.IndexOf(entryToFind) != -1);
    //m = File.ReadLines(fileName).Where(l => l.ToLower().Contains(entryToFind.ToLower())).ToList();
    var m = File.ReadLines(fileName).Where(l => l.ToLower().IndexOf(entryToFind.ToLower()) > -1).ToList();
    //m returns 0 count;
    return m.ToString();        
}
试试这个

public static string GetEntryFromConfigFile(string fileName, string entryToFind)
{
    var m = File.ReadLines(fileName).Where(l => l.StartsWith(entryToFind, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault(); 
    return m;
}
试试这个

public static string GetEntryFromConfigFile(string fileName, string entryToFind)
{
    var m = File.ReadLines(fileName).Where(l => l.StartsWith(entryToFind, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault(); 
    return m;
}

您可以尝试执行以下操作:

  var entry = File.ReadLines(fileName).FirstOrDefault(l => l.IndexOf(entryToFind,StringComparison.CurrentCultureIgnoreCase) >= 0)

这将检索一个条目。它将检查一行是否包含给定的字符串。它忽略大小写和区域性设置

您可以尝试执行以下操作:

  var entry = File.ReadLines(fileName).FirstOrDefault(l => l.IndexOf(entryToFind,StringComparison.CurrentCultureIgnoreCase) >= 0)
这将检索一个条目。它将检查一行是否包含给定的字符串。它忽略大小写和区域性设置

使用
StartsWith()
IndexOf()
不是一个好主意。如果文件中有两行以
keydemo1
keydemo11
开头,该怎么办

这就是我要做的:

public static string GetEntryFromConfigFile(string fileName, string entryToFind)
{
    return File.ReadLines(filename).FirstOrDefault(line => line.Split(',')[0].Equals(entryToFind, StringComparison.CurrentCultureIgnoreCase));
}
使用
StartsWith()
IndexOf()
不是一个好主意。如果文件中有两行以
keydemo1
keydemo11
开头,该怎么办

这就是我要做的:

public static string GetEntryFromConfigFile(string fileName, string entryToFind)
{
    return File.ReadLines(filename).FirstOrDefault(line => line.Split(',')[0].Equals(entryToFind, StringComparison.CurrentCultureIgnoreCase));
}

您是否有任何错误,或者根本没有结果?您是否有任何错误,或者根本没有结果?非常感谢。Mod:File.ReadLines(fileName.FirstOrDefault(line=>line.Split('^')[0].ToLower().StartsWith(entryToFind.ToLower(),StringComparison.CurrentCultureIgnoreCase));谢谢。Mod:File.ReadLines(fileName.FirstOrDefault(line=>line.Split('^')[0].ToLower().StartsWith(entryToFind.ToLower(),StringComparison.CurrentCultureIgnoreCase));