c# - Is it possible to store, or pre-define a LINQ query for re-use? -


i have linq query use 5 times within same mvc class, possible define query somewhere within page in same way can public const string or int, without having create method calls linq query?

i.e.

const linqquery myquery = cat in db.categories cat.categoryid != null select cat;  ...  public actionresult edit(long id = 0) {     viewbag.parentcategoryid = myquery;     ... }  public actionresult create() {     viewbag.parentcategoryid = myquery;     ... } 

from can see, way creating method, avoid if there nicer way of doing things.

my crystal ball tells me linq queries executed when iterate on result so:

static list<int> list = new list<int> { 1, 2, 3 }; static ienumerable<int> result = in list > 2 select i;  static void main(string[] args) {     console.writeline(result.sum()); // 3     list.add(5);     console.writeline(result.sum()); // 8 } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -