【问题标题】:How to take data from var in radio button list如何从单选按钮列表中的var获取数据
【发布时间】:2013-12-30 13:02:10
【问题描述】:

我正在使用 linQ to sql,我的 .cs 页面代码是

var rrlist = db.sel_aut_rec(12); //stored procedure

if (rrlist.Count() != 0)
{
rblist1.DataSource = rrlist;  //radio button list
rblist1.DataTextField ="tbl1.j_title";
rblist1.DataValueField ="tbl1.ed_journal_id";
rblist1.DataBind();
}

我的存储过程代码是

ALTER PROCEDURE dbo.sel_aut_rec
@auth_id int
AS
 select tbl1.ed_journal_id,tbl1.j_title from tbl_aut_rec as tbl inner join editor_j_inf as tbl1 on tbl.j_id=tbl1.ed_journal_id where tbl.auth_id=@auth_id order by tbl.aut_rec_id desc

RETURN

我不知道如何从包含许多行的 rrlist1 中获取特定列

我的问题解决了

var rrlist = db.sel_aut_rec(12).tolist();

【问题讨论】:

    标签: c# asp.net linq stored-procedures linq-to-sql


    【解决方案1】:

    你的方法应该是,

    1. 更改您的 SP 并选择 Id、Name 或任何唯一标识单选按钮的内容。 在你的情况下假设 Auth_Id 和 Auth_name。
    2. 将这两个设置到您的列表中。

    SP:

    ALTER PROCEDURE dbo.sel_aut_rec
    @auth_id int
    AS
    BEGIN
    
    select auth_id as Id, auth_name as Name 
    from tbl_aut_rec as tbl 
      inner join editor_j_inf as tbl1 
        on tbl.j_id=tbl1.ed_journal_id 
    where auth_id=@auth_id order by aut_rec_id desc
    
    END
    

    CS:

    var rrlist = db.sel_aut_rec(Convert.ToInt32(Request.Cookies["authorid"].Value); //stored procedure
    
    if (rrlist !=null)
    {
    rblist1.DataSource = rrlist;  
    rblist1.DataTextField =  "Id"  // You should pick Id and Name 
                                   //{or whatever suits your need} so as 
                                   //  to set the text and value
    rblist1.DataValueField = "Name"
    rblist1.DataBind();
    }
    

    【讨论】:

    • 查看我编辑的问题,我已经尝试过这个并收到错误“System.InvalidOperationException:查询结果不能多​​次枚举。” at rrlist1.databind();
    • @Alps :实际上 Count() 已经枚举了一次。您必须删除 count()。否则,您需要使用 var x = rrlist.ToArray() 和 if 使用 x.Length > 0 将其转换为数组。但不强制您继续检查。由你决定。
    【解决方案2】:

    select * 将从您的 all 表中获取所有 cloumns,因此您需要为

    提供正确的列名和列 ID
    rblist1.DataTextField = "ColumnName" //For display purpose like description field
    
    rblist1.DataValueField = "ColumnName" //For storing in DB like id field name 
    

    请看一下这个sample

    【讨论】:

    • 查看我编辑的问题,我已经尝试过了,但错误来了“System.InvalidOperationException:查询结果不能被枚举多次。”
    【解决方案3】:

    你也可以使用 Linq 来代替你的存储过程:

    var rrlist = dbo.tbl_aut_rec.Join(dbo.editor_j_inf, a => a.j_id, e => e.ed_journal_id, (a, e) => new { Author = a, Editor e})
    .Where(AuthorAndEditor => AuthorAndEditor.Author.auth_id.Equals(Convert.ToInt32(Request.Cookies["authorid"].Value))
    .OrderBy(AuthorAndEditor => AuthorAndEditor.Author.aut_rec_id).ToList();
    
    if (rrlist.Count() != 0)
    {
    rblist1.DataSource = rrlist;  //radio button list
    rblist1.DataTextField = "Id"  //how to take particular column from rrlist1 containing many row
    rblist1.DataValueField = "Name" //how to take particular column from rrlist1 containing many row
    rblist1.DataBind();
    }
    

    我用记事本写得很快,所以可能有错字。

    【讨论】:

    • 查看我编辑的问题,我已经尝试过这个并收到错误“System.InvalidOperationException:查询结果不能多​​次枚举。” at rrlist1.databind();
    • 你能试着编辑我做的吗?我已经有一段时间没有在 Linq 中做过这样的事情了。
    • 你能不能把这个linq查询变成SP,这样我就可以在不同的页面重复使用它了吗?
    • 它是否按预期工作?我建议使用这个 Linq 查询的类,它基本上可以让你做同样的事情。 1> 创建一个静态类(例如:ServiceManager.cs) 2> 创建一个具有相应名称的静态方法 3> 在应用程序的不同页面中使用该类。
    猜你喜欢
    • 2010-12-17
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    相关资源
    最近更新 更多