Linq in Function
-
Hi all, I just started to learn about linq and have some question. say normally we would query something like this bt01DataContext db = new bt01DataContext("Data Source=SOURCE;Initial Catalog=XXX;User ID=XX;Password=XX;"); var query = from b in db.CATEGORies select b; foreach (var cat in query) { ... } now, I want split it into one separate class so that i will have the query all together in 1 file say Query.cs. public DATA TYPE** GetAllCategories() { try { var query = from b in db.CATEGORies select b; return query; } catch (Exception ex) { throw ex; } } in my code I want to call something like this Query q = new Query; var sampleQuery = q.GetAllCategories(); question: Do I have to use something else to replace the var? and what return DATA TYPE** should I use in order to get the result I want? i read from somewhere that i could use bt01(I have bt01.dbml) as data type but I couldn't get it to work when I try that.. any information would be greatly appreciated... thx guys
-
Hi all, I just started to learn about linq and have some question. say normally we would query something like this bt01DataContext db = new bt01DataContext("Data Source=SOURCE;Initial Catalog=XXX;User ID=XX;Password=XX;"); var query = from b in db.CATEGORies select b; foreach (var cat in query) { ... } now, I want split it into one separate class so that i will have the query all together in 1 file say Query.cs. public DATA TYPE** GetAllCategories() { try { var query = from b in db.CATEGORies select b; return query; } catch (Exception ex) { throw ex; } } in my code I want to call something like this Query q = new Query; var sampleQuery = q.GetAllCategories(); question: Do I have to use something else to replace the var? and what return DATA TYPE** should I use in order to get the result I want? i read from somewhere that i could use bt01(I have bt01.dbml) as data type but I couldn't get it to work when I try that.. any information would be greatly appreciated... thx guys
The return type I believe would be
IEnumerable<Category>
. You dont need to replace var. -
The return type I believe would be
IEnumerable<Category>
. You dont need to replace var. -
Hi all, I just started to learn about linq and have some question. say normally we would query something like this bt01DataContext db = new bt01DataContext("Data Source=SOURCE;Initial Catalog=XXX;User ID=XX;Password=XX;"); var query = from b in db.CATEGORies select b; foreach (var cat in query) { ... } now, I want split it into one separate class so that i will have the query all together in 1 file say Query.cs. public DATA TYPE** GetAllCategories() { try { var query = from b in db.CATEGORies select b; return query; } catch (Exception ex) { throw ex; } } in my code I want to call something like this Query q = new Query; var sampleQuery = q.GetAllCategories(); question: Do I have to use something else to replace the var? and what return DATA TYPE** should I use in order to get the result I want? i read from somewhere that i could use bt01(I have bt01.dbml) as data type but I couldn't get it to work when I try that.. any information would be greatly appreciated... thx guys
I got one more question that I think I have missed out. in this code: var query = from b in db.CATEGORies select b; foreach (var cat in query) { ... } in the code "var cat", what is the data type of that var if i need to pass that value to other function? again, thank you very much.
-
I got one more question that I think I have missed out. in this code: var query = from b in db.CATEGORies select b; foreach (var cat in query) { ... } in the code "var cat", what is the data type of that var if i need to pass that value to other function? again, thank you very much.
Well the query will return a IEnumerable<Category> type so in a foreach the cat type is Category.
-
The return type I believe would be
IEnumerable<Category>
. You dont need to replace var.The return type would actually be IQueryable<Category> but as it is based on IEnumerable<Category> they are "interchangeable" (depending on you needs of course).
...Of course, we're talking about software development here, where the simplest solution is often ignored..