I have following linq
var data = repository.FindAll().Where(x => x.Country.ID == ID).ToList();
var serializedData = (from c in data select new { ID = c.ID, Name = c.Name });
how can I this serializedData query simplify (combine) with first query?
Well you should almost certainly get rid of the ToList()
call, to start with - that way the selection part will be turned into SQL and avoid pulling down unnecessary data. You can do the whole thing in a query expression:
var serializedData = from x in repository.FindAll()
where x.Country.ID == ID
select new { x.ID, x.Name };
Note that if FindAll()
returns an IEnumerable<T>
rather than IQueryable<T>
, you should see if there's another method which does return an appropriate IQueryable<T>
- otherwise you're fetching the whole table from the database. (At least, I assume there's a database involved... you haven't given us much context.)
See more on this question at Stackoverflow