1

I want to create global function, but I have an error in mycode like this :

public static GetTotalResearchByArea()
{
    var listOfStrings = new List<int>();
    var all_area = context.Areas.Select(x => x.Id ).ToList();

    foreach( var item in all_area)
    {
        var allReserachCategory = context.ResearchCategories
                                         .Where(a => a.AreaId == item)
                                         .Select(a => a.Id).ToList();

        var totalResearchBySingleArea = context.Researchs
                  .Where(c => allReserachCategory.Contains(c.ResearchCategoryId)).Count();

        listOfStrings.Add(totalResearchBySingleArea);
    }
    return listOfStrings;
}

what should it be ?

Presto
  • 888
  • 12
  • 30
  • It seems that you have not set the type of `GetTotalResearchByArea`. You need to have it to be something like `public static Task> GetTotalResearchByArea()` and then `return Task.FromResult(listOfStrings);`. It could also just return a `List` straight on - `public static List GetTotalResearchByArea`. – jwweiler Jan 17 '19 at 07:43
  • ok thanks alot, I will try it. – muhammad iqbal Mubarok Jan 17 '19 at 07:49
  • Possible duplicate of [Global functions in MVC 5](https://stackoverflow.com/questions/27262259/global-functions-in-mvc-5) – Liam Jan 17 '19 at 10:08

3 Answers3

3

alhamdulillaah solved..

public Array GetTotalResearchByArea()
{
    var list = new List<int>();
    var all_area = context.Areas.Select(x => x.Id ).ToList();

    foreach( var item in all_area)
    {
        var allReserachCategory = context.ResearchCategories.Where(a => a.AreaId == item).Select(a => a.Id).ToList();
        var totalResearchBySingleArea = context.Researchs.Where(c => allReserachCategory.Contains(c.ResearchCategoryId)).Count();
        list.Add(totalResearchBySingleArea);
    }
    return list.ToArray();
}
Presto
  • 888
  • 12
  • 30
1

If you are defining something its better to use IList than List<> . As an answer for your question I would prefer to use

public static IList<int> GetTotalResearchByArea(){
 //write your codes as usual
}

NiteshLama
  • 41
  • 6
0

You are returning string from the function but there is no return type of function update code like this:

public static List<int> GetTotalResearchByArea()
{ 
    // here put your code
}

Hope it helps.

Presto
  • 888
  • 12
  • 30
Nabeel
  • 58
  • 11