【问题标题】:Get a Property value in a BindingSource LINQ在 BindingSource LINQ 中获取属性值
【发布时间】:2012-11-19 20:44:26
【问题描述】:

下面是我的 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 };

我想获取当前记录的UserID。

var userRecord = pics_Data_HistoryBSForCounts.Current;

我将当前记录作为字符串获取。有没有办法获取属性值?

谢谢!

【问题讨论】:

    标签: c# .net linq datasource bindingsource


    【解决方案1】:

    BindingSource.Current 是一个object。您可以将其转换为您的类型 T IQueryable&lt;T&gt; (dataCtx.Pics_Data_Histories)。但那是匿名类型,因此使用命名类型更容易(否则只能使用反射来提取属性值)。

    所以,假设您有一个类UserData(带有UserNameCountUserID)。您查询的select 部分是:

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

    还有演员:

    var userRecord = (UserData)pics_Data_HistoryBSForCounts.Current;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      相关资源
      最近更新 更多