简体   繁体   English

将查询结果返回到列表c#

[英]returning query result to a list c#

I am working on a project where I am phasing out Entity framework from an existing system. 我正在开发一个项目,我将从现有系统中逐步淘汰实体框架。

I got 我有

public List<GSP> GetOwnAirline()
        { 
            var res = from own in entity.GSP where own.Description == "Own Airline" select own;
            return res.ToList();
        }

To bypass this i did 绕过这个我做了

   public List<GSP> GetOwnAirline()
        {

            string get_ = "SELECT * FROM " + Adm.schema_ + "Adm.GSP WHERE Description = 'Own Airline'";
            ccs = new SqlConnection(Adm.COnnectionString);
            //convey transaction to db
            cmd = ccs.CreateCommand();
            ccs.Open();
            cmd.CommandText = get_;
            var res =cmd.ExecuteScalar();
            ccs.Close();

            return res.ToList();
        }

But the.ToList seems not to be recognised in this situation. 但是.ToList似乎在这种情况下不被认可。 Where did i go wrong guys ? 我哪里出错了?

ExecuteScalar returns a scalar - like an integer value. ExecuteScalar返回一个标量 - 就像一个整数值。 It can't be converted to a list. 它无法转换为列表。

If you're using Ado.Net you have to either return a DataTable or a DataReader to get the result. 如果您使用的是Ado.Net,则必须返回DataTable或DataReader才能获得结果。 There is no way to return a List directly. 无法直接返回List。

There are few problems with your code. 您的代码几乎没有问题。

  1. ExecuteScalar returns a single value as pointed out by TGH. ExecuteScalar返回TGH指出的单个值。
  2. Basically, you use ToList when you have some sort of Array or IEnumerable. 基本上,当你有某种Array或IEnumerable时,你使用ToList。 As, in your case, it is a single int value, so, it can't be applied here. 在您的情况下,它是单个int值,因此,它不能在此处应用。
  3. You can use ExecuteReader here to accomplish your goal. 您可以在此处使用ExecuteReader来实现目标。

Try this: 尝试这个:

    public List<GSP> GetOwnAirline()
    {
        List<GSP> lstGSP = new List<GSP>();
        using (ccs = new SqlConnection(Adm.COnnectionString))
        {
            string get_ = "SELECT * FROM " + Adm.schema_ + "Adm.GSP WHERE Description = 'Own Airline'";
            using (SqlCommand cmd = new SqlCommand(get_, ccs))
            {

                ccs.Open();
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (rdr.Read())
                {
                    GSP objGSP = new GSP();
                    Fill(objGSP, rdr);//method
                    lstGSP.Add(objGSP);
                }
                ccs.Close();
            }
        }

        return lstGSP;
    }



public static void Fill(object LogicObject, System.Data.SqlClient.SqlDataReader SqlDataReader)
        {
            Dictionary<string, PropertyInfo> props = new Dictionary<string, PropertyInfo>();
            foreach (PropertyInfo p in LogicObject.GetType().GetProperties())
                props.Add(p.Name, p);
            //foreach (System.Data.DataColumn col in Row.Table.Columns)

            for (int i = 0; i < SqlDataReader.FieldCount; i++)
            {
                string name = SqlDataReader.GetName(i);
                if (SqlDataReader[name] != DBNull.Value && props.ContainsKey(name))
                {
                    object item = SqlDataReader[name];
                    PropertyInfo p = props[name];
                    if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                        if (p.PropertyType != SqlDataReader.GetFieldType(i))
                            item = Convert.ChangeType(item, p.PropertyType.GetGenericArguments()[0]);

                    }
                    else
                    {
                        if (p.PropertyType != SqlDataReader.GetFieldType(i))
                            item = Convert.ChangeType(item, p.PropertyType);
                    }

                    p.SetValue(LogicObject, item, null);
                }

            }
        }

Exucute scalar will return you scalar. Exucute标量将返回标量。 Therefore you will have to create the object of type GSP and add to list. 因此,您必须创建GSP类型的对象并添加到列表中。 an alternate way of doing this can be 这样做的另一种方法是

public List<GSP> GetOwnAirline()
    {

        string get_ = "SELECT * FROM " + Adm.schema_ + "Adm.GSP WHERE Description = 'Own Airline'";
        ccs = new SqlConnection(Adm.COnnectionString);
        //convey transaction to db
        cmd = ccs.CreateCommand();
        ccs.Open();
        cmd.CommandText = get_;
        var res =cmd.ExecuteScalar();
        ccs.Close();
        List<GSP> gsp = new List<GSP>();
        GSP temp = new GSP();
        temp.PropertyName = res;
        gsp.Add(temp);
        return gsp;
    }

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

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