Asp.net mvc5 两个匹配的字符串 return false?

Asp.net mvc5 two matching strings return false?

在我比较两个字符串的项目中,字符串如何匹配但由于某种原因 returns false...

这是我尝试运行比较的代码:

    @using (Html.BeginForm("Index", "Projects", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    {
        var allProjects = ViewData["allProjects"] as List<Project>;
        <h3>
            <a href="#">Inhouse projekt</a>
        </h3>
        <div>
            @{
                Html.RenderPartial("Projects", allProjects.Where(x => x.ProjectStatu.Name == "Pågående - Inhouse"));
            }
        </div>
        <h3>
            <a href="#">Outhouse projekt</a>
        </h3>
        <div>
            @{
                Html.RenderPartial("Projects", allProjects.Where(x => x.ProjectStatu.Name == "Pågående - Outhouse"));
            }
        </div>
        <h3>
            <a href="#">Övriga projekt</a>
        </h3>
        <div>
            @{
                Html.RenderPartial("Projects", allProjects.Where(x => x.ProjectStatu.Name != "Pågående - Inhouse" && x.ProjectStatu.Name != "Pågående - Outhouse" && x.ProjectStatu.Name != "Avslutat"));
            }
        </div>
        <h3>
            <a href="#">Avslutade projekt</a>
        </h3>
        <div>
            @{
                Html.RenderPartial("Projects", allProjects.Where(x => x.ProjectStatu.Name == "Avslutat"));
            }
        </div>
        <input type="submit" value="Spara" id="submit" name="submit" style="padding: 5px 20px 5px 20px; float: right;" />
     }
 }

证明这两个字符串确实匹配,但 returns 是错误的..

尝试使用 allProjects.Where(x => x.ProjectStatu.Name.Contains("Inhouse"))

与其这样进行比较,不如使用 string.Compare。 link 那里还包括文化信息,这可能与您的情况相关。

allProjects.Where(x => string.Compare(x.ProjectStatu.Name, {your string}, false, CultureInfo.InvariantCulture);

我这么说是因为我很容易看出这两个字符串的 Unicode 格式有问题。这应该可以解决这个问题。