7

I want to create a list of empty IQueryable and change it to ToPagedList, I tried the following code :-

IQueryable<VirtualMachine> vm2 = new IQueryable<VirtualMachine>();
vm2.ToPagedList(page, pagesize);

but it will raise the following exception:-

Error 3 A new expression requires (), [], or {} after type

Joe
  • 7,113
  • 1
  • 29
  • 34
  • Why the same question twise?? [How to create empty ToPagedList items inside my action method and pass them to my view](http://stackoverflow.com/questions/20067998/how-to-create-empty-topagedlist-items-inside-my-action-method-and-pass-them-to-m) – huMpty duMpty Nov 19 '13 at 11:40
  • Why do you want this? – Jens Kloster Nov 19 '13 at 11:42
  • in my case a requirement has come that I need to display records from other system , so I will be populating these values as viewData, and for my system record I will be creating empty Iquerable ,, so that I can reuse my current index view . –  Nov 19 '13 at 11:59

3 Answers3

14

You could use something like this

 IQueryable<VirtualMachine> vm2 = new VirtualMachine[] {}.AsQueryable();

Do you actually want / need this though? as on MSDN, the IQueryable interface is for use on data sources.

Provides functionality to evaluate queries against a specific data source wherein the type of the data is known.

I would decide whether or not you actually need this before implementing it like this.

Joe
  • 7,113
  • 1
  • 29
  • 34
  • but this will create only one item inside the vm2 , while I need to create 10 empty items inside the vm2 ? is there a way to do so ? –  Nov 20 '13 at 09:31
  • So initialise 10 items in side the [collection initialiser](http://msdn.microsoft.com/en-us/library/vstudio/bb384062.aspx), again though, you probably really don't need this. – Joe Nov 20 '13 at 09:47
2

You can't create an instance of an interface - there's no implementation

Dave
  • 3,581
  • 1
  • 22
  • 25
1

You are trying to create an instance of an interface not an object. THis cannot be done.

Have a look at this SO question

What instantiate-able types implementing IQueryable are available in .Net 4.0?

Paying special attention to this

IQueryable objects are produced by Queryable Providers (ex. LINQ to SQL, LINQ to Entities/Entity Framework, etc). Virtually nothing you can instantiate with new in the basic .NET Framework implements IQueryable.

Community
  • 1
  • 1
David Pilkington
  • 13,528
  • 3
  • 41
  • 73