我在课堂上有这个功能
public System.Collections.IList cpv_No_Date_list()
{
var cpvNoDateList = (from s in context.DB.CPVs
select new { s.cpv_no, s.cpv_date} ).ToList();
return cpvNoDateList;
}
然后我调用此函数并将其作为下拉列表的源
var Service = IOC.Get<IEntityListService>();
var cpvNoDateSrc = Service.cpv_No_Date_list();
cpvDrpdwn.DataSource = cpvNoDateSrc;
cpvDrpdwn.DataBind();
函数是从表中收集精确数据,但问题是它没有正确呈现它。 下拉列表中的输出如下:
{cpv_no = 5, cpv_date = 1/2/2013}
{cpv_no = 7, cpv_date = 5/2/2013}
{cpv_no = 8, cpv_date = 8/2/2013}
如何摆脱输出中的这些括号
答案 0 :(得分:0)
您将看到匿名类的默认ToString
方法
创建一个新类并覆盖ToString
public class CpvNoAndDate
{
public int cpv_no { get; set; }
public DateTime cpv_date { get; set; }
public override string ToString()
{
return string.Format("cpv_no = {0}, cpv_date = {1}", cpv_no, cpv_date);
}
}
然后像这样投射到你的新课:
public System.Collections.IList cpv_No_Date_list()
{
var cpvNoDateList = (from s in context.DB.CPVs
select new CpvNoAndDate
{
cpv_no = s.cpv_no,
cpv_date = s.cpv_date
}).ToList();
return cpvNoDateList;
}