4

This is my code:

SomeFunction(m => { 
    ViewData["AllEmployees"].Where(c => c.LeaderID == m.UserID); 
 })

and it returns this error:

Not all code paths return a value in lambda expression of type System.Func<IEnumerable>

0

4 Answers 4

13

Assuming you're trying to return the result of that .Where() query, you need to drop those braces and that semicolon:

SomeFunction(m => ViewData["AllEmployees"].Where(c => c.LeaderID == m.UserID))

If you put them there, ViewData[...].Where() will be treated as a statement and not an expression, so you end up with a lambda that doesn't return when it's supposed to, causing the error.

Or if you insist on putting them there, you need a return keyword so the statement actually returns:

SomeFunction(m =>
{
    return ViewData["AllEmployees"].Where(c => c.LeaderID == m.UserID);
})
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! It was careless of me. Thank you!
4

You can either write lambda's body as an expression:

SomeFunction(m => ViewData["AllEmployees"].Where(c => c.LeaderID == m.UserID))

or as a statement:

SomeFunction(m => { 
    return ViewData["AllEmployees"].Where(c => c.LeaderID == m.UserID); 
})

Comments

1

Simply do

SomeFunction(m => ViewData["AllEmployees"].Where(c => c.LeaderID == m.UserID));

Comments

0

There are a couple of open questions about your codebase.. doing wild assumptions i think this is the rigth answer:

        SomeFunction(
            (m) =>
            {
                return ViewData["AllEmployees"].Where(
                       (c) => { return (c.LeaderID == m.UserID); });
            });

and here is why:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace App
{
    public class DataSet
    {
    }

    public class someclass
    {
        public DataSet Where(Func<Person, Boolean> matcher)
        {
            Person anotherone = new Person();
            matcher(anotherone);
            return new DataSet();
        }
    }

    public class Person
    {
        public string LeaderID, UserID;
    }

    class Program
    {
        public static Dictionary<String, someclass> ViewData;

        private static void SomeFunction(Func<Person, DataSet> getDataSet)
        {
            Person thepersonofinterest = new Person();
            DataSet thesetiamusinghere = getDataSet(thepersonofinterest);
        }

    static void Main(string[] args)
    {
        SomeFunction(
            (m) =>
            {
                return ViewData["AllEmployees"].Where(
                       (c) => { return (c.LeaderID == m.UserID); });
            });
    }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.