首页 > 解决方案 > 返回记录的方法 true false 或 all

问题描述

有没有更好的方法来编写下面的方法?我想根据bool变量返回内容IsPublished

我什么时候想返回既是IsPublished和的条目。nulltruefalse

public IList<Content> GetContent(bool? IsPublished)
{
    if (IsPublished != null)
    {
        return _UoW.Content.All.Where(c => c.IsPublished == IsPublished).ToList();
    }
    else
    {
        return _UoW.Content.All.ToList();
    }
}

标签: c#linqmethods

解决方案


如果更好,你的意思是更短,运营商?可以成为解决方案。此外,当isPublished == null. 请参阅此处的 Microsoft 文档以了解?操作员。

public IList<Content> GetContent(bool? IsPublished)
{
    return isPublished != null ? _UoW.Content.All.Where(c=>c.IsPublished== IsPublished).ToList() : _UoW.Content.All.ToList();
}

推荐阅读