简体   繁体   English

返回记录为true或全部的方法

[英]Method to return records true false or all

Is there a better way to write the method below? 有没有更好的方法来编写下面的方法? I want to return content based on a bool variable IsPublished . 我想基于bool变量IsPublished返回内容。

When IsPublished is null I want to return entries that are both true and false . IsPublishednull我想返回truefalse条目。

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();
    }
}

If by better, you mean shorter, operator ? 如果更好,您是说较短,运算符? can be the solution. 可以解决。 Also, there is no point using filter when the isPublished == null . 另外,当isPublished == null时,使用filter毫无意义。 Please refer to Microsoft Documentation here for ? 在此处参考Microsoft文档? operator. 操作员。

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

This is not as efficient, but is compact: 这不是很有效,但是紧凑:

public IList<Content> GetContent(bool? IsPublished)
    => _UoW.Content.All.Where(c => !IsPublished.HasValue || c.IsPublished == IsPublished).ToList();


You can use the null coalesce operator ?? 您可以使用null合并运算符?? and filter on x.IsPublished == (v ?? x.IsPublished) 并过滤x.IsPublished == (v ?? x.IsPublished)

public class A
{
    public int Id { get; set; }
    public Nullable<bool> IsPublished { get; set; }
}

public static IEnumerable<A> Filter(IEnumerable<A> items, Nullable<bool> v)
{
    return items.Where(x => x.IsPublished == (v ?? x.IsPublished)).ToList();
}

var listy = new List<A>();
listy.Add(new A() { Id=1, IsPublished=null });
listy.Add(new A() { Id=2, IsPublished=true });
listy.Add(new A() { Id=3, IsPublished=false });
listy.Add(new A() { Id=4, IsPublished=null });
listy.Add(new A() { Id=5, IsPublished=true });
listy.Add(new A() { Id=6, IsPublished=false });

C# interactive console demo: C#交互式控制台演示:

> Filter(listy, null)
List<Submission#0.A>(6) { Submission#0.A { Id=1, IsPublished=null }, Submission#0.A { Id=2, IsPublished=true }, Submission#0.A { Id=3, IsPublished=false }, Submission#0.A { Id=4, IsPublished=null }, Submission#0.A { Id=5, IsPublished=true }, Submission#0.A { Id=6, IsPublished=false } }
> Filter(listy, true)
List<Submission#0.A>(2) { Submission#0.A { Id=2, IsPublished=true }, Submission#0.A { Id=5, IsPublished=true } }
> Filter(listy, false)
List<Submission#0.A>(2) { Submission#0.A { Id=3, IsPublished=false }, Submission#0.A { Id=6, IsPublished=false } }  

(The class above is A instead of Content and the method is Filter instead of GetContent ) (上面的类是A而不是Content ,方法是Filter而不是GetContent

If you are trying to be brief, you don't need { } blocks for single statement if else bodies. 如果您想简短一点,那么如果没有else正文,则无需为单个语句if { }块。

But perhaps a single return point is what you are looking for: 但是,也许您正在寻找一个返回点:

public IList<Content> GetContent(bool? IsPublished) {
    var ans = _UoW.Content.All;
    if (IsPublished.HasValue)
        ans = ans.Where(c => c.IsPublished == IsPublished);

    return ans.ToList();
}

(Assuming the type of All is compatible with the return type of Where . If not, you can change the assignment to be var ans = _UoW.Content.All.Where(c => true) .) (假设All的类型与Where的返回类型兼容。如果不兼容,则可以将赋值更改为var ans = _UoW.Content.All.Where(c => true) 。)

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

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