使用linq获取表的两列时出现问题 [英] problem when getting two columns of a table with linq

查看:47
本文介绍了使用linq获取表的两列时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在课堂上有这个功能

i have this function in a class

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;
}





然后我调用此函数并将其作为下拉列表的来源



then i am calling this function and giving it as a source to a drop down list

var Service = IOC.Get<IEntityListService>();
var cpvNoDateSrc = Service.cpv_No_Date_list();

cpvDrpdwn.Items.Add(new ListItem("------------- Select -----------------------------------", "0"));
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}



如何摆脱输出中的这些括号???



function is collecting exact data from table but the problem is that it is not presenting it correctly.
Output in dropdown list is like this:
{cpv_no = 5, cpv_date = 1/2/2013}
{cpv_no = 7, cpv_date = 5/2/2013}
{cpv_no = 8, cpv_date = 8/2/2013}

How to get rid of these brackets in output???

推荐答案

你不能,不是那样的。

问题是你正在创建一个匿名类型,并且匿名类型的默认ToString实现是成员列表,包含在大括号中 - 与您一样。删除括号的唯一方法是声明一个正确类型,并实现其ToString覆盖。

然后在Linq中创建一个新类型的实例 select 而不是匿名类型,它应该全部工作。
You can't, not like that.
The problem is that you are creating an anonymous type, and the default ToString implementation for an anonymous type is the member list, contained in curly brackets - exactly as you have. The only way to remove the brackets is to declare a "proper" type, and implement its ToString override.
You then create a new instance of your type in the Linq select instead of an anonymous type, and it should all work.


你的输出结果没问题。



您需要定义DisplayMember和ValueMember属性或下拉控件,例如:



cpvDrpdwn.DisplayMember =cpv_date;

cpvDrpdwn.ValueMember =cpv_no;

cpvDrpdwn.DataSource = cpvNoDateSrc;

cpvDrpdwn.DataBind();
Your output result is OK.

You need to define DisplayMember and ValueMember Properties or Dropdown Control, like :

cpvDrpdwn.DisplayMember = "cpv_date";
cpvDrpdwn.ValueMember = "cpv_no";
cpvDrpdwn.DataSource = cpvNoDateSrc;
cpvDrpdwn.DataBind();


这篇关于使用linq获取表的两列时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆