0

Update: After several attempts, figured out even String can not be recognized but string.

If I create new project from MVC template, while debugging in immediate window I can do:

var a = new List<int>{1,2,3};
a.First()

However in my existing project, If I try to do same:

var a= new List{1,2,3};

it gives me

error CS0246: The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)

On the other hand, I can do this.

var a = new System.Collections.Generic.List<int>{1,2,3};

but this time

a.First() gives me

error CS1061: 'List<int>' does not contain a definition for 'First' and no accessible extension

I have also on top razor view page

@using System;
@using System.Linq;
@using System.Collections.Generic

In fact, everything is used to working in the past with out doing any changes in web config.

I tried several attempts such as this and this one

What could be wrong with my existing project that I can not use nor List neither Linq expression?

My real case is, I want to use linq lambda expression in my razor view while debugging.


Mystery solved, in my view I have Kendo ui mvc fluent expression:

@(Html.Kendo().Window()
.Modal(true)
.Width(350)
.Height(280)
.Actions(a => a.Custom("ActivateRole"))
.Animation(true)
.Content(FormContent().ToHtmlString())
.Name("RoleSelector")
.Events(eve => eve.Activate("centerKendoWindow"))
.Title("Role selection")
)

form form content, I have

   @helper FormContent()
   {
   }

So for this, I am "loosing context" then all namespaces ignored.

asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84

1 Answers1

0

well I just tried following piece of code in my razor view and it worked fine

@using System;
@using System.Linq;
@using System.Collections.Generic;

@{ 

    var a = new List<int> { 1, 2, 3 };
    a.First();

}

See if the above code will help you. Secondly try using LINQ functions on the controller side just to be sure that your libraries are working fine.

Lastly if you want to find an alternative I would suggest you to create a new c# class. make a new folder e.g. helper and add a c# class, make the class static and write whatever functions you want then reference it in your views. Here is a piece of code that will help you.

public static class CommonMethods
{

    public static int AgendaFileCount(int AgendaID)
    {
        // Some function using linq you want to do
        ApplicationDbContext db = new ApplicationDbContext();
        return db.MeetingAgendaDocument.Where(c => c.MeetingAgendaID == AgendaID).Count();
    }
}

and then in .cshtml view file just reference this class namespace

@using AssestsManagementWebApp.Helpers

and then use it anywhere in your view as follows

@{
  int totalFiles = CommonMethods.AgendaFileCount(item.ID);
 }
Abdul Hannan
  • 424
  • 1
  • 6
  • 20