简体   繁体   中英

Get a Property value in a BindingSource LINQ

Below is my bindingSource.

pics_Data_HistoryBSForCounts.DataSource = from dh in dataCtx.Pics_Data_Histories
                                                        join ui in dataCtx.User_Infos on dh.User_Id equals ui.user_id into ui_dh
                                                        from ui_dh1 in ui_dh.DefaultIfEmpty()
                                                        where dh.Changed_Date >= fromDate && dh.Changed_Date <= toDate
                                                        group ui_dh1 by ui_dh1.user_name into grouped
                                                      select new { UserName = grouped.Key.Trim(), Count = grouped.Count(a => a.user_name != null), UserID = grouped.FirstOrDefault().user_id };

I want to get the UserID of the current record.

var userRecord = pics_Data_HistoryBSForCounts.Current;

I get the current records as a string. Is there any way to get a property value?

Thanks!

BindingSource.Current is an object . You can cast it to type T of you IQueryable<T> (dataCtx.Pics_Data_Histories). But that is an anonymous type, so it is easier to use a named type for it (otherwise you can only use Reflection to extract a property value).

So, suppose you have a class UserData (with UserName , Count and UserID ). The select part of your query is:

select new UserData { UserName = grouped.Key.Trim(), ...

And the cast:

var userRecord = (UserData)pics_Data_HistoryBSForCounts.Current;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM