简体   繁体   English

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

[英]Fill Gridview using LINQ to DataSet query with alias

I'm getting following error. 我遇到以下错误。

A field or property with the name 'P_ID' was not found on the selected data source. 在所选数据源上找不到名称为“ P_ID”的字段或属性。

I want to bind a Gridview using LINQ to DataSet . 我想使用LINQGridview绑定到DataSet I have three tables from which I want theirs ID's . 我有三个要从中获取其ID's In query, I used aliases but it gives me error because the aliases are not found. 在查询中,我使用了别名,但由于找不到别名,因此给了我错误。

this is my code 这是我的代码

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 在所选数据源上找不到名称为“ P_ID”的字段或属性

The message is self-explanatory, this property is not available in the class that you have used, but apparently you are trying to bind to this property. 该消息是不言自明的,该属性在您使用的类中不可用,但是显然您正在尝试绑定到该属性。

Since you are selecting this column in the sql query but assign it to the property ID you should bind to that property instead. 由于您正在sql查询中选择此列,但将其分配给属性ID您应该绑定到该属性。

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

Side-Note: you could use a verbatim string literal, it makes it much easier to write a sql query in C#. 旁注:您可以使用逐字字符串文字,这样可以更轻松地用C#编写sql查询。 I would also suggest to use real/ansi joins. 我也建议使用真实/ ansi连接。 Even "Oracle recommends that you use the FROM clause OUTER JOIN syntax rather than the Oracle join operator" link . 甚至“ Oracle建议您使用FROM子句的OUTER JOIN语法,而不是Oracle join运算符” 链接

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM