简体   繁体   English

asp.net mvc5的两个匹配字符串是否返回false?

[英]Asp.net mvc5 two matching strings return false?

Working on a project where I compare two string, how ever the string do match but it returns false for some reason... 在比较两个字符串的项目上工作,该字符串如何匹配,但由于某种原因它返回false。

This is the code I try to run to compare: 这是我尝试运行以比较的代码:

    @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;" />
     }
 }

Proof the two string's do match but it returns a false.. 证明两个字符串确实匹配,但返回false。 在此处输入图片说明

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

Instead of doing your comparison like that, instead you should use string.Compare . 与其那样进行比较, 不如使用string.Compare The link there also includes the Culture Info, which will probably be relevant in your case. 那里的链接还包括文化信息,这可能与您的情况有关。

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

I say this because I could easily see there being a problem with the Unicode formats of the two strings. 我之所以这样说,是因为我很容易看到两个字符串的Unicode格式存在问题。 This should get around that. 这应该可以解决。

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

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