简体   繁体   English

简化If语句C#

[英]Simplify If statement C#

I'm trying to generate a count for values coming in from a database call. 我正在尝试为来自数据库调用的值生成计数。 I am using an if statement to go through each value. 我正在使用if语句来遍历每个值。 I am a newbie to programming and I can help feeling that there is a shorter way to do this, any suggestions will be greatly appreciated. 我是编程的新手,可以帮助我感觉到有一种更短的方法可以做到,任何建议将不胜感激。

This is my data call: 这是我的数据通话:

public List<MarkingOverviewDTO> GetMarkingOverview(int? myModuleID)
{
    List<MarkingOverviewDTO> MyMarkingOverview = new List<MarkingOverviewDTO>();

    using (var context = new StudentPortalDBEntities1())
    {
        var myOverview =
            from m in context.ModuleDetails
            join ms in context.MarkingScheduleOverviews on m.ModuleID equals ms.ModuleFk
            join mot in context.MarkingOverviewTitles on ms.MSOverview equals mot.OverviewTitleID
            where m.ModuleID == myModuleID
            select new
            {
                mot.TitleName,
                ms.Comment1,
                ms.Comment2,
                ms.Comment3,
                ms.Comment4,
                ms.Comment5,
                ms.Comment6,
                ms.Comment7,
                ms.Comment8,
                ms.Comment9,
                ms.Comment10,

            };

        foreach (var item in myOverview)
        {
            MyMarkingOverview.Add(new MarkingOverviewDTO
            {
                OverviewTitle = item.TitleName,
                Comment1 = item.Comment1,
                Comment2 = item.Comment2,
                Comment3 = item.Comment3,
                Comment4 = item.Comment4,
                Comment5 = item.Comment5,
                Comment6 = item.Comment6,
                Comment7 = item.Comment7,
                Comment8 = item.Comment8,
                Comment9 = item.Comment9,
                Comment10 = item.Comment10,
                //Get the comment count to generate the correct number of checkboxes per row
                MyCommentCount = CountComments(item.Comment1, item.Comment2, item.Comment3, item.Comment4, item.Comment5, item.Comment6, item.Comment7, item.Comment8, item.Comment9, item.Comment10),
            });
        }
    }

    return MyMarkingOverview;
}

This is my method: 这是我的方法:

/// <summary>
/// Get comment count based on value. This will return a count to generate the correct number of check boxes  per overview
/// </summary>
/// <param name="Comment1"></param>
/// <param name="Comment2"></param>
/// <param name="Comment3"></param>
/// <param name="Comment4"></param>
/// <param name="Comment5"></param>
/// <param name="Comment6"></param>
/// <param name="Comment7"></param>
/// <param name="Comment8"></param>
/// <param name="Comment9"></param>
/// <param name="Comment10"></param>
/// <returns></returns>
public int CountComments(string Comment1, string Comment2, string Comment3, string Comment4, string Comment5, string Comment6, string Comment7, string Comment8, string Comment9, string Comment10)
{
    var myCommentCount = 0;

    if (Comment1 != null)
    {
        myCommentCount += 1;
    }

    if (Comment2 != null)
    {
        myCommentCount += 1;
    }

    if (Comment3 != null)
    {
        myCommentCount += 1;
    }

    if (Comment4 != null)
    {
        myCommentCount += 1;
    }

    if (Comment5 != null)
    {
        myCommentCount += 1;
    }

    if (Comment6 != null)
    {
        myCommentCount += 1;
    }

    if (Comment7 != null)
    {
        myCommentCount += 1;
    }

    if (Comment8 != null)
    {
        myCommentCount += 1;
    }

    if (Comment9 != null)
    {
        myCommentCount += 1;
    }

    if (Comment10 != null)
    {
        myCommentCount += 1;
    }

    return myCommentCount;
}
public int CountComments(params string[] comments)
{
  return comments.Count(x => x != null);
}

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

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