如何使用条件语句返回IEnumerable Linq值

时间:2014-05-06 13:21:24

标签: c# linq

我在"if... else if..." Linq查询中有一个条件IEnumerable语句。我收到以下错误:

  

并非所有代码路径都返回值

以下是我的代码。

public IEnumerable<SASF> GetLongReportData(string commSubGp)
        {

            var context = new Entities();
            string myDate = "2014-03-18";
            DateTime date = Convert.ToDateTime(myDate);



            if (commSubGp == "F00")
            {


                var getAgriculture = from a in context.SASF                
                                     where a.RDate == date &&
                                     a.COMM_SGP.CompareTo("F00") <= 0
                                     orderby a.Conmkt, a.MKTTITL descending
                                     select a;

                return getAgriculture.ToList();
            }
            else if (commSubGp == "n10")
            {

                var getPetroleum = from p in context.SASF
                                   where p.RDate == date &&
                                   p.COMM_SGP == "n10"
                                   orderby p.Conmkt, p.MKTTITL descending
                                   select p;

                return getPetroleum.ToList();
            }

            return ??????;     // what should be here?   
        }

6 个答案:

答案 0 :(得分:3)

典型的方法是返回null,空的可枚举Enumerable.Empty<SASF>()或投掷ArgumentException,具体取决于您的需要。

如果&#34; F00&#34;和&#34; n10&#34;是commSubGp可以拥有的唯一有效值,您应该为此参数抛出ArgumentException。在这种情况下,编译器将停止要求返回。

更干净:将此参数设为枚举或布尔值。例如,枚举ReportType仅包含值AgriculturePetroleum

答案 1 :(得分:2)

您正在查询过滤后几乎相似的查询和相同列的排序。如果使用switch语句添加特定过滤器并且如果有人提供不支持的参数则抛出未实现的异常,则可以使代码更容易维护,同样必须以某种方式处理null参数的情况。

public IEnumerable<SASF> GetLongReportData(string commSubGp)
{
    var context = new Entities();
    var date = Convert.ToDateTime("2014-03-18");
    var sasf = (from s context.SASF
                where a.RDate == date
                select s);

    if (!String.IsNullOrEmpty(commSubGp))
    {
        switch (commSubGp)
        {
            case "F00":
                sasf = (from s in sasf
                        s.COMM_SGP.CompareTo("F00") <= 0
                        select s);
                break;

            case "n10":
                sasf = (from s in sasf
                        s.COMM_SGP == "n10"
                        select s);
                break;

            default:
                throw new NotImplementedException(String.Format("commSubGp {0} not implemented", commSubGp));
        }
    }
    else
    {
        throw new ArgumentNullException("Parameter commSubGp is null");
    }

    return sasf.OrderBy(p => p.Conmkt).ThenByDescending(p => p.MKTTITL).ToList();      
}

答案 2 :(得分:1)

我想这取决于你真正想要回归的内容

return Enumerable.Empty<SASF>();

return null;

或抛出异常......

答案 3 :(得分:0)

return new List<SASF>(); 

或异常或null。

答案 4 :(得分:-1)

实际上并不难。在if情况之上创建结果并返回。

您的默认结果是空的

对于其他案例和/或未来案例,创建一个案例,根据正确的标准填写案例

 public IEnumerable<SASF> GetLongReportData(string commSubGp)
    {

        var context = new Entities();

        //ALSO Change
        //string myDate = "2014-03-18";
        //DateTime date = Convert.ToDateTime(myDate);

        var date = new DateTime(2014,3,18);

        //Create a result in the type you want to return.
        var result = new List<SASF>();

        switch(commSubGp)
        {
            case "F00": //Agriculture

                result = (from a in context.SASF                
                             where a.RDate == date &&
                             a.COMM_SGP.CompareTo(commSubGp) <= 0
                             orderby a.Conmkt, a.MKTTITL descending
                             select a).ToList();

                break;
            case "n10": //petroleum
                result = (from p in context.SASF
                           where p.RDate == date &&
                           p.COMM_SGP == commSubGp
                           orderby p.Conmkt, p.MKTTITL descending
                           select p).ToList();
                break;
        }

        return result;     
    }

答案 5 :(得分:-2)

将新变量定义为

IEnumerable<SASF> sasf = null;

并返回return sasf;

public IEnumerable<SASF> GetLongReportData(string commSubGp)
{
  IEnumerable<SASF> sasf;
  if(condition)
  {
    //your stuff and put it in sasf
  }
  else if(condition)
  {
    //your stuff and put it in sasf
  }
  return sasf;
}