我想在.net MVC4 C#中創建列表過濾器。 我有ajax查詢發送字符串到控制器,並根據數據庫中的匹配,它返回記錄數。c#字符串比較
所以當String
爲IsNullOrEmpty()
和IsNullOrWhiteSpace()
它帶給我良好的效果。 我現在在匹配值時遇到問題。
雖然它似乎我容易,所以我tried-
控制器
public ActionResult SearchAccountHead(string accountHead)
{
var students = from s in db.LedgerTables
select s;
List<LedgerModel> ledge = null;
if (!String.IsNullOrEmpty(accountHead))
{
//Returns non-empty records
}
if (String.IsNullOrEmpty(accountHead) && String.IsNullOrWhiteSpace(accountHead))
{
//Checks whether string is null or containing whitespace
//And returns filtered result
}
return PartialView(ledge);
}
現在,如果我有字符串,不字符串我一直在使用的控制器匹配,那麼我想它映射 -
if (String.IsNullOrEmpty(accountHead) && String.IsNullOrWhiteSpace(accountHead) && !String.Compare(accountHead))
if (String.IsNullOrEmpty(accountHead) && String.IsNullOrWhiteSpace(accountHead) && !String.Compare(AccountHead,ledge.AccountHead))
但是在這兩種情況下它都不起作用。
如何在字符串不匹配時進入第二種方法?
'!String。比較(AccountHead)'不會編譯 – Jonesopolis
另外,「在情況A和情況B這麼做」的情況下,真的意味着「如果A或(如果)B」並且在C#中被寫爲「if(A‖B){DoThis ); } –
'String.IsNullOrEmpty(x)&& String.IsNullOrWhiteSpace(x)'與'String.IsNullOrWhiteSpace(x)'相同。爲了簡潔起見,我會推薦後者。 –