我想在.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)'相同。为了简洁起见,我会推荐后者。 –