LinqToSql main question: why is this not compiling?

Recently, I introduce myself to LinqToSQL through a poorly executed project at work. I am curious why this works:

var territories = db.Territories.Where(t => t.PendingUserCount > 0); 

But this leads to a compilation error:

 var territories = db.Territories; if (someCondition) territories = territories.Where(t => t.PendingUserCount > 0); // Cannot implicitly convert 'System.Linq.IQueryable<Territory> to System.Data.Linq.Table<Territory> 

I also tried calling db.Territories.ToList() , but to no avail.

I'm sure this is just a misunderstanding of how Linq works, but I would appreciate it if someone would help me.

+4
source share
9 answers

Alternative:

 var territories = db.Territories.AsQueryable(); if (someCondition) territories = territories.Where(t => t.PendingUserCount > 0); 
+5
source

db.Territories returns a table object. Therefore, "var" will be of type System.Data.Linq.Table. Later, you try (based on some condition) to assign a variable of type System.Linq.IQueryable to a variable. Since .NET is strongly typed, the compiler throws an error.

Variables of type var will be assigned to types when they are assigned first. This is how I try to remember myself.

+8
source

For this type of cumulative Where you need to tell the compiler to use IQueryable<T> :

 IQueryable<Territory> territories = db.Territories; if (someCondition) territories = territories.Where(t => t.PendingUserCount > 0); ... etc 
+7
source

change to this

 var territories = db.Territories; 

to

 IQueryable territories = db.Territories.Where(t => t.PendingUserCount > 0); 

The reason is that by calling db.Territories, you are returning all the data back by returning it to the linq.table object. Db.Territores.where (...) will return an IQueryable object instead.

+3
source

One of the potentially confusing things about "var" is that its type is determined at compile time, so you cannot assign it a number of different types. People with experience with dynamic languages ​​like Python sometimes get confused at first.

+2
source

Your var territories is entered as System.Data.Linq.Table<Territory> initially, and then you try to assign the result Where (which is of type System.Linq.IQueryable<Territory> ).

Remember that the compiler indicates the type of destination var when assigning, so the type cannot be changed after it is assigned.

Try something like this:

 System.Linq.IQueryable<Territory> territories; if (someCondition) territories = db.Territories.Where(t => t.PendingUserCount > 0); 
+1
source

Since you typed "var" in the form of a <Territory > table, then try reassigning it as IQueryable <Territory >.

This is equivalent to saying

  var i = 0 i = "a string"; 

EDIT: To clarify, var is implicitly typed at compile time rather than run time, unlike a dynamically typed scripting language.

0
source

You need to use IQueryable tyupe as others suggested:

It is also possible to execute this linq request:

 var query = from territories in db.Territories where territories.SomeProperty == SomeCondition where territories.PendingUserCount > 0 select territories; 
0
source

You cannot reassign var to another type after you declare it. So your var declaration already typed it in System.Data.Linq.Table.

This is fire once.

0
source

All Articles