using (var sqlConnection = new SqlConnection(connectionString)) { sqlConnection.Open(); foreach (var deliveryNumber in deliveryNumbers) { var images = new List<byte[]>(); // get all ewtphotos for this sapdeliverynumber using (var sqlCommand = new SqlCommand()) { sqlCommand.Connection = sqlConnection; sqlCommand.CommandType = CommandType.Text; sqlCommand.CommandText = "select imagedata " + "from delivery d " + "inner join image i on d.imageid=i.imageid " + "where d.deliverynumber='" + sapDeliveryNumber + "'"; using (var sqlDataReader = sqlCommand.ExecuteReader()) { while (sqlDataReader.Read()) { var imageData = (byte[])sqlDataReader["ImageData"]; var imageExists = images.Any(image => image.SequenceEqual(imageData)); if (!imageExists) images.Add(imageData); } } } WriteImagesToDisk(sapDeliveryNumber, images); } } private static void WriteImagesToDisk(string deliveryNumber, List<byte[]> images) { var counter = 1; foreach (var image in images) { var fileFullName = @"images\DeliveryNumber_" + deliveryNumber + "_" + counter + ".jpg"; File.WriteAllBytes(fileFullName, image); counter++; } }
Wednesday, 4 April 2012
Comparing Byte Arrays with Linq
This morning I ran into a small issue. I needed to extract a series of images from a database table. The problem I had was there were duplicate images in the database table and I needed a unique set of images written to disk. To filter the duplicates I have used the Enumerable.SequenceEqual linq operator. So here is how I did it.
Subscribe to:
Post Comments (Atom)
This worked for me. Great post! Thanks.
ReplyDelete