C # Anonymous type foreach loop, best way?

I have this simple foreach anonymous type, and I am wondering if there is a way to make it more efficient.

If it goes through 155 elements, it takes about 20 seconds to complete. I omitted some other properties that it sets for the boAsset object, but nothing special - just Strings / Integers .

Any thoughts?

 List<BoAsset> assetList = new List<BoAsset>(); foreach (var asset in result) { BoAsset boAsset = new BoAsset(); boAsset.Description = asset.Description; boAsset.DetailedDescription = asset.DetailedDescription; boAsset.AssetCustomerID = asset.AssetCustomerID; boAsset.AssetId = asset.AssetId; boAsset.Keywords = asset.Keywords; boAsset.Notes = asset.Notes; boAsset.Photographer = asset.Photographer; boAsset.PhotographerEmail = asset.PhotographerEmail; boAsset.Notes = asset.Notes; boAsset.Author = asset.Author; boAsset.FileName = asset.FileName; boAsset.FileExtension = asset.FileExtension; boAsset.AssetCreateDate = asset.AssetCreateDate; boAsset.AssetExpireDate = asset.AssetExpireDate; assetList.Add(boAsset); } var query = (from a in context.Assets join subAf1 in context.AssetFiles on new { aid = a.AssetID, ftid = 1 } equals new { aid = subAf1.AssetID, ftid = subAf1.AssetTypeID } into theAf1 from Af1 in theAf1.DefaultIfEmpty() join subAf2 in context.AssetFiles on new { aid = a.AssetID, ftid = 2 } equals new { aid = subAf2.AssetID, ftid = subAf2.AssetTypeID } into theAf2 from Af2 in theAf2.DefaultIfEmpty() join subAf3 in context.AssetFiles on new { aid = a.AssetID, ftid = 3 } equals new { aid = subAf3.AssetID, ftid = subAf3.AssetTypeID } into theAf3 from Af3 in theAf3.DefaultIfEmpty() join subAf4 in context.AssetFiles on new { aid = a.AssetID, ftid = 4 } equals new { aid = subAf4.AssetID, ftid = subAf4.AssetTypeID } into theAf4 from Af4 in theAf4.DefaultIfEmpty() join subAf5 in context.AssetFiles on new { aid = a.AssetID, ftid = 5 } equals new { aid = subAf5.AssetID, ftid = subAf5.AssetTypeID } into theAf5 from Af5 in theAf5.DefaultIfEmpty() join subFp in context.FilePaths on new { fpid = Af1.FilePathID } equals new { fpid = subFp.FilePathID } into theFp1 from fp1 in theFp1.DefaultIfEmpty() //join fp in context.FilePaths on Af1.FilePathID equals fp.FilePathID where a.AssetCustomerID == custId && a.AssetID == assetId select new { a, Af1, Af2, Af3, Af4, Af5, fp1 }).Distinct(); var result = from q in query select new { AssetId = qaAssetID, AssetCustomerId = qaAssetCustomerID, StockImage = qaStockImage, Description = qaDescription, DetailedDescription = qaDetailedDescription, Author = qaAuthor, FileName = q.Af1.FileName, //was 1 FileExtension = q.Af1.FileExtension, //was 1 AssetCreateDate = qaAssetCreateDate, AssetExpireDate = qaAssetExpireDate, AssetActivateDate = qaAssetActivateDate, Notes = qaNotes, Keywords = qaKeywords, Photographer = qaPhotographer, PhotographerEmail = qaPhotographerEmail } 
+2
performance c # foreach linq anonymous-types
source share
2 answers

The time to create 155 objects and copy data into them will be something like a millisecond. There is nothing you could do with this code to make any significant (or even noticeable) improvement.

If you want to save all the time, you should look at the query that creates the result. This is what takes time.

+2
source share

I think your request is too complex (too many joins), especially considering that there are several that are not even used.

Note that some LINQ providers (such as LINQ to Entities) can take quite some time just processing the query tree to convert it to SQL if the query is complex. (I already had a few queries in 10 or 15 seconds just for EF to parse it.)

If, as I suspect, the LINQ implementation you are using gets into the database, then use the SQL profiling tool to check which actual SQL is passed to the server and how efficient it is in that SQL.

Also, note that given the fields you use, you could actually drop the foreach by materializing the results directly into BoAsset objects.

I.e:

 var result = from q in query select new BoAsset { AssetId = qaAssetID, AssetCustomerId = qaAssetCustomerID, StockImage = qaStockImage, Description = qaDescription, DetailedDescription = qaDetailedDescription, Author = qaAuthor, FileName = q.Af1.FileName, //was 1 FileExtension = q.Af1.FileExtension, //was 1 AssetCreateDate = qaAssetCreateDate, AssetExpireDate = qaAssetExpireDate, AssetActivateDate = qaAssetActivateDate, Notes = qaNotes, Keywords = qaKeywords, Photographer = qaPhotographer, PhotographerEmail = qaPhotographerEmail }; List<BoAsset> assetList = result.ToList(); 
+2
source share

All Articles