Finding Which Crawled Property Data Types Match a Managed Property Data Type in SharePoint 2010

Not sure how often this will come up, but it was something that took me a while to track down so I thought I'd share here.  Suppose you are working with a ManagedProperty in SharePoint search, and you want to look at mapping it to a crawled property.  Now a managed property usually just has one of about 5 basic data types:  Text, Integer, Decimal, DateTime or YesNo.  That ManagedType property of a ManagedProperty doesn't have a corresponding "thing" on a CrawledProperty though.  Fortunately, there is a helper function.  The Schema class as a static method called IsMappable.  You pass to it a ManagedDataType, which a ManagedProperty uses, and a VariantType, which a CrawledProperty uses.  The net is a list of CrawledProperty items with a compatible data type for a ManagedProperty.  Here's a quick little LINQ snippet I wrote to retrieve a list of CrawledProperty items that could be used with a ManagedProperty:

//look for all crawled properties that match the data type of our mapped property

props = from CrawledProperty cp in c.GetAllCrawledProperties
where Schema.IsMappable(DataType, cp.VariantType)
select cp;

 

The fabulous formatting effects are free courtesy of this awesome blog software we have here.  In this case, "c" represents a Category, which I also got from the Schema class - mySchemaInstance.AllCategories.