Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Linq AnonymousType Problem
 

Linq AnonymousType Problem

Hi.
I have a big table and i want to show some filed using linq query.
   List<DA.Tel> lst = PublicDA.Db.Tels.OrderBy(p => p.orderNumber).ToList()                        
Then I bind this query to my DataGridview.Now I When user click on grid I want to know which record is selected and doing some action .
I use CurrencyManager to control binding and find current record;
object source = selectQuery();
CurrencyManager       cur = (CurrencyManager)this.BindingContext[source];
dg.DataSource = source;
So when i want to do some action to current record,for example if i select from Tel table:
 if (cur.Position > -1)
         Tel   entity= (Tel)cur.Current;


It work fine when we select from my one table,But mostly ,I want to select a few field,or select from two table,
  var query = from d in PublicDA.Db.Tels
                        where d.TelActionID == actID
                        orderby d.RegisterDate descending
                        select new
                        {
                            d.FirstName,
                            d.LastName,
                            d.TelNumber,
                            d.Mobile,
                            d.Address,
                            d.Dsc,
                            d.id,
                            d.EnteryDate,
                            d.RegisterDate
                        };

In this case because the linq query type is AnonymousType I have Problem to cast it.
I use this method in so many form and I want to find a good solution?
Any idea.
saeed_sp  Tuesday, August 18, 2009 12:12 PM
You may be able to create a genericextension method to do this.

I did not try this, but something like this might work:

T Cast<T>(object obj, T type)
{
  return (T)obj;
}<br/>
Let me know if this works for you.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, August 18, 2009 2:48 PM
You may be able to create a genericextension method to do this.

I did not try this, but something like this might work:

T Cast<T>(object obj, T type)
{
  return (T)obj;
}<br/>
Let me know if this works for you.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, August 18, 2009 2:48 PM
And how about query that returns some fields from two or more table? What type I should to cast?
saeed_sp  Tuesday, August 25, 2009 5:19 AM

You can use google to search for other answers

Custom Search

More Threads

• Getting Stored Proc Return Code from TableAdapter
• Table Adapter timeout
• How to get a DataGridViewTextBoxCell to act like multiline textbox without wordwrap?
• displays records from two different tables
• How to group related column-headers with a main column-header in a DataGridView ?
• Regional Settings
• DataGridView ComboBox column - formatted items
• ConstraintException when modifying column values in a DataView
• Showing more then one table in DataGridView
• How to retrive BLOB?