Answered by:
Function that returns the results of a linq query.

Question
-
User-305496339 posted
I was just wondering what is best practice for writing a function that returns the results of a linq query. What should
be the type of the function? Thanks for any advice anyone can provide.Monday, September 25, 2017 4:38 PM
Answers
-
User475983607 posted
rkrex
I was just wondering what is best practice for writing a function that returns the results of a linq query. What should
be the type of the function? Thanks for any advice anyone can provide.The results of a linq query is a collection of types; IEnumerable<myType> or List<myType>. The query is invoked when enumerated.
Do you have a snippet of code you can post that is causing you a problem?
Otherwise see the Linq reference documentation.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 25, 2017 6:41 PM -
User1655654435 posted
The return type of a method that performs a linq query can be IQueryable if you are not sending it to the database yet(it can also be other things).
The purpose of linq is to build a query, meaning build the logic of what you want to do. Then when you are ready to do something with the data, you can execute it by telling it what data you intend to get back(using methods that are "keywords" for that). like ToList(). The query will then return that type if possible(like IEnumerable).
returns a query without going to db: public IQueryable<Person> GetSome(string name) { return _context.Persons.Where(firstName => firstName.FirstName == name).OrderBy(a => a.Age); } builds a query, goes to db to get it and puts it into a list: public List<Person> GetSome2(string name) { return _context.Persons.Where(firstName => firstName.FirstName == name).OrderBy(a => a.Age).ToList(); }
I'm currently learning linq myself. I think that if one is good at it you can become really good at working with databases, doing very effective and low cost data operations.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, September 26, 2017 8:11 AM -
User1120430333 posted
Thanks for your help. I was wondering in the case of executing a stored procedure like the one below, what would I return? Thanks Again !
DataClasses1DataContext db = new DataClasses1DataContext();
var q = db.spGetEmployeeById(3).ToList();;you would return a dto or dto's in a collection using the dto pattern
https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 29, 2017 5:36 AM
All replies
-
User475983607 posted
rkrex
I was just wondering what is best practice for writing a function that returns the results of a linq query. What should
be the type of the function? Thanks for any advice anyone can provide.The results of a linq query is a collection of types; IEnumerable<myType> or List<myType>. The query is invoked when enumerated.
Do you have a snippet of code you can post that is causing you a problem?
Otherwise see the Linq reference documentation.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 25, 2017 6:41 PM -
User-305496339 posted
Thanks for your help. I tried your advice and its working fine.
Monday, September 25, 2017 7:26 PM -
User1655654435 posted
The return type of a method that performs a linq query can be IQueryable if you are not sending it to the database yet(it can also be other things).
The purpose of linq is to build a query, meaning build the logic of what you want to do. Then when you are ready to do something with the data, you can execute it by telling it what data you intend to get back(using methods that are "keywords" for that). like ToList(). The query will then return that type if possible(like IEnumerable).
returns a query without going to db: public IQueryable<Person> GetSome(string name) { return _context.Persons.Where(firstName => firstName.FirstName == name).OrderBy(a => a.Age); } builds a query, goes to db to get it and puts it into a list: public List<Person> GetSome2(string name) { return _context.Persons.Where(firstName => firstName.FirstName == name).OrderBy(a => a.Age).ToList(); }
I'm currently learning linq myself. I think that if one is good at it you can become really good at working with databases, doing very effective and low cost data operations.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, September 26, 2017 8:11 AM -
User-305496339 posted
Thanks for your help. I was wondering in the case of executing a stored procedure like the one below, what would I return? Thanks Again !
DataClasses1DataContext db = new DataClasses1DataContext();
var q = db.spGetEmployeeById(3).ToList();;Tuesday, September 26, 2017 3:28 PM -
User475983607 posted
Thanks for your help. I was wondering in the case of executing a stored procedure like the one below, what would I return? Thanks Again !
DataClasses1DataContext db = new DataClasses1DataContext();
var q = db.spGetEmployeeById(3).ToList();;A List<T> where T is the complex object you defined when creating the function import.
Tuesday, September 26, 2017 5:15 PM -
User1120430333 posted
Thanks for your help. I was wondering in the case of executing a stored procedure like the one below, what would I return? Thanks Again !
DataClasses1DataContext db = new DataClasses1DataContext();
var q = db.spGetEmployeeById(3).ToList();;you would return a dto or dto's in a collection using the dto pattern
https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 29, 2017 5:36 AM