我试图从LINQ查询返回的匿名类型创建BindingList <>但BindingList <>不接受匿名类型,以下是我的代码
var data = context.RechargeLogs.Where(t => t.Time >= DateTime.Today).
Select(t => new
{
col1 = t.Id,
col2 = t.Compnay,
col3 = t.SubscriptionNo,
col4 = t.Amount,
col5 = t.Time
});
var tmp = new BindingList<???>(data);
Run Code Online (Sandbox Code Playgroud)
在最后一行泛型参数放置什么???
你可以写一个扩展方法:
static class MyExtensions
{
public static BindingList<T> ToBindingList<T>(this IList<T> source)
{
return new BindingList<T>(source);
}
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
var query = entities
.Select(e => new
{
// construct anonymous entity here
})
.ToList()
.ToBindingList();
Run Code Online (Sandbox Code Playgroud)
如果您需要在其他地方使用此对象,我建议使用dynamic,或者甚至更好,简单地将您需要的对象创建为struct.
public class RechargeLogData
{
public int Id { get; set; }
public string Company { get; set; }
public string SubscriptionNo { get; set; }
public string Amount { get; set; }
public string Time { get; set; }
}
var data = context.RechargeLogs.Where(t => t.Time >= DateTime.Today).
Select(t => new RechargeLogData()
{
Id = t.Id,
Company = t.Compnay,
SubscriptionNo = t.SubscriptionNo,
Amount = t.Amount,
Time = t.Time
});
var tmp = new BindingList<RechargeLogData>(data);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1684 次 |
| 最近记录: |