开发者

Databinding to LINQ query result in Silverlight

开发者 https://www.devze.com 2023-02-16 17:24 出处:网络
I am retrieving simple LINQ query, but I am joining with two table and binding data with ListBox. I am not able to properly show the item into the ListBox.

I am retrieving simple LINQ query, but I am joining with two table and binding data with ListBox. I am not able to properly show the item into the ListBox.

once I remove new item and select only keyword using it will work properly, but I want to join two table with select new key word it wil not allow to bind data with ListBox. my code is like. This will not allow to bind with ListBox.

var newPeople = (from p in clsGeneral.db.Table<SmartFXAttribes>()
           开发者_C百科      join q in clsGeneral.db.Table<CategoryAttribes>() on p.catId equals q.ID
                 where p.catId == ((SmartFX.CategoryAttribes)((ComboBox)cmbPrintSize).SelectedValue).ID
                 select new 
                 {
                     p.ID,
                     p.ImageHeight,
                     p.Imageoutline,
                     p.ImageUnit,
                     p.ImageWidth,
                     p.NoofPic,
                     p.TextboxCaption,
                     p.CanvasPixelHeight,
                     p.CanvasPixelWidth,
                     p.CanvasUnit,
                     p.catId,
                     q.FileName
                 }).ToList();
lstThumbnail.ItemsSource = newPeople;

This code will work fine.

var newPeople =
    (from p in clsGeneral.db.Table<SmartFXAttribes>()
     join q in clsGeneral.db.Table<CategoryAttribes>() on p.catId equals q.ID
     where p.catId == ((SmartFX.CategoryAttribes)((ComboBox)cmbPrintSize).SelectedValue).ID
     select p).ToList();

lstThumbnail.ItemsSource = newPeople;

Thanks!


The problem is that the first query creates an anonymous-typed object, but Silverlight cannot do data binding against an anonymous-typed object (anonymous types are internal and Silverlight's reflection capabilities do not allow accessing internal types from other assemblies). Your second query returns objects of a named type so it works just fine.

The best solution to this is to declare a public type containing public properties for everything you want to return from your first query and return an instance of that instead.

You can work around it with this hack, though.

0

精彩评论

暂无评论...
验证码 换一张
取 消