none
if else LINQ RRS feed

  • Question

  • Hi, I am using LINQ in Entity Framework to retrieve Text when id parameter is passed, when no id selected it has to return all the data with Code, Text columns. Not able to return the values based on my condition.


    Error: Not all code paths return a value

    private DbEntities db = new DbEntities(); public class CodeText { public string Code { get; set; } public string Text { get; set; } } public string GetCodeText(string id) { if (id != null) { var codes = (from c in db.CodeText where c.Code == id select new { Text = c.Text }).Single(); string t = codes.Text; } else { var codes = (from c in db.CodeText where c.Code == id select new { Text = c.Text, Code = c.Code }; }

    }

    Thank you in advance


    SQLEnthusiast


    Tuesday, June 21, 2016 9:04 PM

Answers

  • Hi,

    you cannot return return codes.ToList() because your function returns a string.

    You either can change the return of your function to List<EFWCodeText> GetCodeText(string id)

    but you need to change the return in your if clause to

     if (id != null)
                {
                    var codes = (from c in db.CodeText
                                where c.Code == id
                                select new
                                {
                                    Text = c.Text
                                });
                    return codes.ToList(); // a list containing one item
                }

    or change the the return in else to be something like that

    var codes = (from c in db.CodeText
                                where c.Code == id
                                select new
                                {
                                    Text = c.Text,
    				Code = c.Code
                                };
    string stCodes="";
    codes.ToList().ForEach(x=>{stCodes+=x.Text+","}); // return a comma separated string
    
    if(stCodes.length>0)
    stCodes=stCodes.Remove(stCodes.Length-1);// remove last ","
    
    return stCodes;


    Thanks,

    Mahmoud


    Tuesday, June 21, 2016 11:55 PM
  • Hi SQLEnthusiast007,

    It seems that we could use string.join method to achieve it. like this:

    var codes = db.CodeTexts.Select(c => c.Code).ToList();
    
    return string.Join(",", codes.ToArray());

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Wednesday, June 22, 2016 2:12 AM
    Moderator
  • Hi,

    I see you are using ASP.net Web API. In the route of Web API in the project, please make the UrlParameter optional to enable passing it empty or not passing it at all.


    Thanks Mahmoud

    Wednesday, June 22, 2016 3:53 PM

All replies

  • Hi,

    Your function returns a string and you have two paths in the function

    1. If

    2. Else

    You need to return a string in the if clause, so your last statment should be 

    return t;

    In the else part, you need your where to be

    "where id==null" to make sure it will return all items. You can make a string containing all codes comma separated.


    Thanks Mahmoud


    Tuesday, June 21, 2016 9:57 PM
  • Hi, Thanks for your response. Per your suggestion I modified my else statement to below and still get an error at return.

    Error: Cannot implicitly convert type List to string

    			else
                {
                    List<EFWCodeText> rolesList = new List<EFWCodeText>();
    
                    var codes = (from c in db.CodeText
                                select new EFWCodeText
                                {
                                    Code = c.Code.ToString() + "; " + c.Text.ToString()
                                }).ToList();
                    return codes.ToList(); //Error here
                }

    Please guide me.

    Thank you.


    SQLEnthusiast

    Tuesday, June 21, 2016 11:14 PM
  • Hi,

    you cannot return return codes.ToList() because your function returns a string.

    You either can change the return of your function to List<EFWCodeText> GetCodeText(string id)

    but you need to change the return in your if clause to

     if (id != null)
                {
                    var codes = (from c in db.CodeText
                                where c.Code == id
                                select new
                                {
                                    Text = c.Text
                                });
                    return codes.ToList(); // a list containing one item
                }

    or change the the return in else to be something like that

    var codes = (from c in db.CodeText
                                where c.Code == id
                                select new
                                {
                                    Text = c.Text,
    				Code = c.Code
                                };
    string stCodes="";
    codes.ToList().ForEach(x=>{stCodes+=x.Text+","}); // return a comma separated string
    
    if(stCodes.length>0)
    stCodes=stCodes.Remove(stCodes.Length-1);// remove last ","
    
    return stCodes;


    Thanks,

    Mahmoud


    Tuesday, June 21, 2016 11:55 PM
  • Hi SQLEnthusiast007,

    It seems that we could use string.join method to achieve it. like this:

    var codes = db.CodeTexts.Select(c => c.Code).ToList();
    
    return string.Join(",", codes.ToArray());

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Wednesday, June 22, 2016 2:12 AM
    Moderator
  • Hi, Thanks all for your response. I tried both the solutions from Mahmoud Tolba & Cole Wu, when this is passed "api/CodeText/" to get all the Codes & Text I get the below error. When I pass this with some code id "api/CodeText/AX90847593D" then it works as expected.

    Error:

    No HTTP resource was found that matches the request URI 'http://localhost/api/CodeText/'.
    No action was found on the controller 'CodeText' that matches the request.

    Regards.


    SQLEnthusiast

    Wednesday, June 22, 2016 3:46 PM
  • Hi,

    I see you are using ASP.net Web API. In the route of Web API in the project, please make the UrlParameter optional to enable passing it empty or not passing it at all.


    Thanks Mahmoud

    Wednesday, June 22, 2016 3:53 PM
  • Thank you Mahmoud Tolba & cole wu.

    SQLEnthusiast

    Wednesday, June 22, 2016 4:36 PM