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

Fill Gridview using LINQ to DataSet query with alias

我遇到以下错误。

A field or property with the name 'P_ID' was not found on the selected data source.

我想使用 LINQGridview 绑定到 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();

A field or property with the name 'P_ID' was not found on the selected data source

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

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

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

旁注:您可以使用逐字字符串文字,这样可以更轻松地在 C# 中编写 sql 查询。我还建议使用 real/ansi 连接。甚至 "Oracle recommends that you use the FROM clause OUTER JOIN syntax rather than the Oracle join operator" 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";