0

I got dbset for table Functions in database and FunctionsContext: dbContext. I am implementing repository. In my interface I have only one function at the movement "GetFunctions". I got stuck in implementing class; method "GetFunctions" where I need to call FunctionsContext to get all list of available functions title from database and then send to controller class

I am using mvc5 asp.net and entity framework

dbContext

public class FunctionsContext : dbContext
{
    public DbSet<App_Functions> Functions { get; set; }
}

model

[Table("Functions")]
public class App_Functions
{
    [Key]
    public int Function_ID { get; set; }

    [StringLength(50)]
    [Required]
    public string Title { get; set; }

    public int Hierarchy_level { get; set; }
}

Domain Class

public class Functions
{
    public Functions()
    {

    }

    public int Function_ID { get; set; }
    public string Title { get; set; }
    public int Hierarchy_level { get; set; }
}

IRepository

interface IFunctionRepository: IDisposable
{
    IQueryable<Functions> GetFunctions { get; }
}

IRepository Implementation class

public class FunctionRepository : IFunctionRepository
{
    private FunctionsContext fun_Context = new FunctionsContext();

    public IQueryable<Functions>GetFunctions
    {
      ?????????
    }
}

what I want to implement in IQueryableGetFunctions

using (var db = new FunctionsContext())
{
    var query = from b in db.Functions
                orderby b.Function_ID
                select b;

    foreach (var item in query)
    {
        var a2 = item.Title;
    }
}
4
  • Where did you get stuck? Commented Dec 17, 2013 at 5:56
  • in IRepository Implementation class Commented Dec 17, 2013 at 6:00
  • in last section i have mansion what I want to implement in IQueryableGetFunctions Commented Dec 17, 2013 at 6:01
  • So what is the problem? What doesn't work? I don't understand what is the question... Commented Dec 17, 2013 at 6:05

2 Answers 2

1

I think the easiest way will be the following:

public IQueryable<Functions> GetFunctions()
{
    return fun_Context.Functions.Select(x=>new Functions {
                   Function_ID = x.Function_ID,
                   Title = x.Title,
                   Hierarchy_level = x.Hierarchy_level 
               });
}
Sign up to request clarification or add additional context in comments.

2 Comments

this is giving me error on return; "A Get or Set Accessor is Expected"
Sorry, forgot () near the method name
1

You have to add () after the method name, this declaration does not work 'public IQueryable GetFunctions'

IRepository Implementation class

public class FunctionRepository : IFunctionRepository
{
    private FunctionsContext fun_Context = new FunctionsContext();

    // For method declaration add the () after the method name
    public IQueryable<Functions> GetFunctions()
    {
        return fun_Context.Functions;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.