使用带别名的LINQ to DataSet查询填充Gridview

时间:2015-09-18 07:26:19

标签: c# asp.net linq linq-to-dataset

我收到了以下错误消息。

  

名称为' P_ID'的字段或媒体资源未在选定的上找到   数据来源。

我想使用GridviewLINQ绑定到DataSet。我有三张桌子,我想要他们ID's。在查询中,我使用了别名,但它给了我错误,因为找不到别名。

这是我的代码

string filterSO = "SELECT " +
                         "P.ID AS P_ID, " +
                         "S.ID AS S_ID, " +                             
                         " RS.LASTNAMER | | ' ' | | RS.FIRSTNAMER AS ReferentName, " +
                         " RS.ID," +
                         " P.STATUSP" +
                        " FROM PLANNING P," +
                         " SHIPPING S," +                            
                         " REFERENT_SHIPPING RS" +                             
                       " WHERE S.ID_REFERENT = RS.ID(+)" +
                       " AND S.ID_PLANNING = P.ID" +
                       " ORDER BY P.ID DESC";
    using (OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["DBCS"].ToString()))
    {
        con.Open();
        OracleCommand cmd = new OracleCommand(filterSO, con);
        OracleDataAdapter da = new OracleDataAdapter(cmd);
        DataSet dss = new DataSet();
        da.Fill(dss, "office_all");
        Session["DATASET"] = dss;

        var officee_all = from xx in dss.Tables["office_all"].AsEnumerable()
                      select new guards
                      {
                          ID = Convert.ToInt32(xx["P_ID"]),
                          ID_S = Convert.ToInt32(xx["S_ID"]),                           

                          LASTNAME_R = xx["LASTNAMER"].ToString(),
                          FIRSTNAME_R = xx["ReferentName"].ToString(),
                          ID_R = Convert.ToInt32(xx["ID"]),
                          STATUSP = xx["STATUSP"].ToString()
                      };

        GridViewSOFirst.DataSource = officee_all.ToList();
        GridViewSOFirst.DataBind();

1 个答案:

答案 0 :(得分:0)

  

名称为' P_ID'的字段或媒体资源未在选定的上找到   数据来源

该消息不言自明,此属性在您使用的类中不可用,但显然您正在尝试绑定到此属性。

由于您在sql查询中选择此列但将其分配给属性ID,您应该绑定到该属性。

ID = Convert.ToInt32(xx["P_ID"])

Side-Note:您可以使用逐字字符串文字,这样可以更轻松地在C#中编写SQL查询。我还建议使用real / ansi连接。甚至" Oracle建议您使用FROM子句OUTER JOIN语法而不是Oracle连接运算符" link

string filterSO = @"SELECT
                     P.ID AS P_ID,
                     S.ID AS S_ID,
                     RS.LASTNAMER | | ' ' | | RS.FIRSTNAMER AS ReferentName, 
                     RS.ID,
                     P.STATUSP
                    FROM PLANNING P,
                     SHIPPING S,
                     REFERENT_SHIPPING RS
                   WHERE S.ID_REFERENT = RS.ID(+)
                   AND S.ID_PLANNING = P.ID
                   ORDER BY P.ID DESC";