Convert linq request to icollection

I need to take the query results:

var query = from m in db.SoilSamplingSubJobs where m.order_id == id select m; 

and prepare as an ICollection so that I can have something like

  ICollection<SoilSamplingSubJob> subjobs 

I'm currently creating a list that does not fit my needs:

 query.ToList(); 

what should I do is query.ToIcollection ()?

+7
list icollection
source share
3 answers

A list is an ICollection. You can change the query.ToList () code as follows.

 query.ToList() as ICollection<SoilSamplingSubJob>; 

You ask how this query is returned as a result of a function. If so, remember that Linq to SQL objects are connected by default, so you will need to control where your database context opens and closes. In addition, you can create DTOs (data transfer objects) that contain the data that you want to use in the rest of your program. These objects can fit into your object hierarchy in any way.

You can also create these DTOs as part of the request.

 var query = from m in db.SoilSamplingSubJobs where m.order_id == id select new SubJobDTO { OrderNumber = m.order_id }; return query.ToList() as ICollection<SubJobDTO>; 
+14
source share

Since the request implements IEnumerable, you can pass it to the collection constructor of your choice. ICollection is an interface implemented by several classes (including List<T> , which is returned by ToList) with various performance characteristics.

0
source share

With a slightly different syntax, you can do something like this that can shorten what is included in ICollection. This approach is suitable for Angular and MVC, when you have a huge table, but you want to upload some details to the cshtml page:

 ICollection<SoilSamplingSubJob> samples = dbContext.GetQuery().Where(m => m.order_id == id) .AsEnumerable().Select(s => new { Id = s.order_id, CustomProperty = s.some_thing }).ToList() as ICollection<SoilSamplingSubJob> 
0
source share

All Articles