在Linq之间添加if语句

时间:2019-05-24 23:22:53

标签: c# linq

我在C#中有这段代码,如何在Linq语句之间添加if语句,在作者之前我需要添加if语句来检查CreatedBy是否为空。

     List<Note> notes = apiNotes.Select(x => new Note()
        {
            noteId = x.NoteId,
            date = x.DateEntered.Date.ToString("MMMM dd"),
            time = x.DateEntered.ToString("h:mm tt"),
            body = x.Text,
            title = x.Title,
           //Where I need to add a if statement like If( x.CreatedBy!="")
            author = new Note.Author()
            {  
                fullname = x.CreatedBy ?? "",
                initial = initials.Replace(x.CreatedBy, "$1") ?? ""
            }
        }).ToList();

3 个答案:

答案 0 :(得分:2)

如果您要排除任何具有空CreatedBy字段的注释,则只需添加一个Where部分。

List<Note> notes = apiNotes
    .Where 
    (
        x => x.CreatedBy != ""
    )
    .Select
    (
        x => new Note()
        {
            noteId = x.NoteId,
            date = x.DateEntered.Date.ToString("MMMM dd"),
            time = x.DateEntered.ToString("h:mm tt"),
            body = x.Text,
            title = x.Title,
            author = new Note.Author()
            {  
                fullname = x.CreatedBy,
                initial = initials.Replace(x.CreatedBy, "$1") ?? ""
            }
        }
    )
    .ToList();

如果要包含所有注释,但如果author没有值,请将CreatedBy属性保留为空,则可以使用内联三元运算符:

List<Note> notes = apiNotes
    .Select
    (
        x => new Note()
        {
            noteId = x.NoteId,
            date = x.DateEntered.Date.ToString("MMMM dd"),
            time = x.DateEntered.ToString("h:mm tt"),
            body = x.Text,
            title = x.Title,
            author = x.CreatedBy == null 
                   ? null
                   : new Note.Author()
                     {  
                        fullname = x.CreatedBy,
                        initial = initials.Replace(x.CreatedBy, "$1") ?? ""
                     }
            }
        }
    )
    .ToList();

答案 1 :(得分:1)

您可以执行以下操作。这是C#中的标准内联条件语法。

 List<Note> notes = apiNotes.Select(x => new Note()
    {
        noteId = x.NoteId,
        date = x.DateEntered.Date.ToString("MMMM dd"),
        time = x.DateEntered.ToString("h:mm tt"),
        body = x.Text,
        title = x.Title,
        author = (!string.IsNullOrEmpty(x.CreatedBy))
               ? new Note.Author() { fullname = x.CreatedBy, 
                                     initial = initials.Replace(x.CreatedBy, "$1") }
               : null
    }).ToList();

但是将构造函数添加到Author()类开始会更有意义。 public Note(ApiNote apinote) { ... } ...

答案 2 :(得分:0)

您可以使用conditional operator

优雅地解决此问题
List<Note> notes = apiNotes.Select(
    x => 
        new Note()
        {
            noteId = x.NoteId,
            date = x.DateEntered.Date.ToString("MMMM dd"),
            time = x.DateEntered.ToString("h:mm tt"),
            body = x.Text,
            title = x.Title,
            author = String.IsNullOrEmpty(x.CreatedBy)
                ? null
                : new Note.Author()
                {  
                    fullname = x.CreatedBy,
                    initial = initials.Replace(x.CreatedBy, "$1") ?? ""
                }
        }
).ToList();
相关问题