LINQ to Collection of Objects

October 21, 2010 at 07:51 (CSharp) (, , , , )

The term “LINQ to Objects” refers to the use of LINQ queries with any IEnumerable or IEnumerable collection directly, without the use of an intermediate LINQ provider or API such as LINQ to SQL or LINQ to XML. You can use LINQ to query any enumerable collections such as List, Array, or Dictionary. The collection may be user-defined or may be returned by a .NET Framework API.
http://msdn.microsoft.com/en-us/library/bb397919.aspx

In this example, I have use the third party library that let’s you modify any swf file (the *Aspose.Flash). In this particular case, I have queried the list of objects in the swf file in two different ways, and retrieve only a particular type (in this case, FlashBitmap & PlaceObject2)

using Aspose.Flash.Swf;
using System.Collections.Generic;
using System.Collections;
using System.Linq;

FlashContainer linqfc = new FlashContainer(_strPath);

//using OfType to get a particular type of object in list
var fcBitmaps = linqfc.Objects.OfType( );

//using LINQ to retrieve particular objects of type PlaceObject2 and filter only
//with Names not equal to null or string.Empty
var po2 = from obj in linqfc.Objects.ToArray( )
where obj.GetType( ).Name == "PlaceObject2"
let placeobj = (PlaceObject2) obj
where placeobj.Name != string.Empty && placeobj.Name != null
select new { placeobj };

foreach( var item in po2 )
{
MessageBox.Show( item.placeobj.ObjectId.ToString( ) + " " + item.placeobj.Name );
}

*for more information about this third party tool, please visit their website, they got a lot of tools that you can use.
http://www.aspose.com/

Permalink Leave a Comment