Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day!
Help plz...

how i can correctly select info from DB...
example...

i have
1 table Cars where have Id, VIN
1 tabke CarImages where I have id, carid (from Car table) and Image as binary data

I need to build sample XML struct:
XML
<Cars>
<Car id="1" vin="CarVin13simbol" PhotosCount="2">
<Car id="2" vin="CarVin13simbol" PhotosCount="10">
<Car id="3" vin="CarVin13simbol" PhotosCount="0">
</Cars>

How correctly build linq query? All i try - download all data (and images too) by var QueryData example
C#
_dc.Cars.First(x => x.id == 1).CarPhotos.Count()

many time my program executing and many memory in use (sometimes outofmemoryexeption)

Help plz... sorry for English... bad know it :)
Posted

1 solution

Try something like this:
C#
var allCars = _dc.Cars.Select(x => new 
{ 
    x.id, 
    x.vin, 
    PhotosCount = x.CarPhotos.Count() 
});

var allCarsXml = allCars.AsEnumerable().Select(x => new XElement("Car",
    new XAttribute("id", x.id),
    new XAttribute("vin", x.vin),
    new XAttribute("PhotosCount", x.PhotosCount)
);

var xml = new XDocument(new XStreamingElement("Cars", allCarsXml));
 
Share this answer
 
v2
Comments
Member 11519571 12-Mar-15 11:00am    
hm... thx for code example...
hm... tell me plz this code:
dc.Cars.First(p => p.Id == CarId).CarPhotos.Count()
download from BD images? Or this cide will be fast?
Richard Deeming 12-Mar-15 16:44pm    
Your code will load the full car details into memory, then load the entire collection of photos for the car into memory, and then count the photos.

The code I posted will be translated into a SQL query that returns the number of images per car, without loading any of the images into your application's memory.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900