Search code examples
c#linq

How can I get data for a list using C# and LINQ


I need to loop though a list of city names and go to database to get the info for each city. I am having problem converting a string to list and vice versa. Below is the code and error messages.

Error 1:

Cannot implicitly convert type 'System.Collections.Generic.List<StateAPI.Models.CityModel>' to 'StateAPI.Models.CityModel'

Error 2:

Cannot convert from 'System.Collections.Generic.List<StateAPI.Models.CityModel>' to 'StateAPI.Models.CityModel'

//Models
namespace StateAPI.Models
{
 public class CityModel: ReadResponse
    {
        public string CityAddress { get; set; }
        public string CityPhone { get; set; }
    }
}
//Controller
namespace StateAPI.Controllers
{

    public class CityController : ApiController
    {
        SQLConnection SQLconn = new SQLConnection();

        public CityModel GetCityInfo(string CityList)
        {

            if (string.IsNullOrEmpty(CityList)
            {
                return new CityModel
                {
                    Message = "No CityList Passed",
                };
            }
            else
            {
                return SQLconn.GetCityMain(CityList);  //Error 1
            }
        }
    }
}
public List<CityModel> GetCityMain(string CityList)
        {
            String strSQL = String.Empty;
            String msg = String.Empty;
            String[] parms = { };

            List<CityModel> output = new List<CityModel>();
            var cities= CityList.Split(',').Select(x => x.Trim());

            foreach (var city in cities)
            {
                strSQL = String.Format(GetCityInfoSQL, city);
                List<CityModel> info = SQLUtil.GetDataList<CityModel>(
                strSQL,
                parms.ToArray<string>(),
                out msg);

                output.Add(info); //Error 2
            }
            return output;
        }
public static List<T> GetDataList<T>(string sql, String[] sqlparams, out String message) where T : new()
        {
            List<T> result = new List<T>();
            message = String.Empty;
            DataSet ds;

            try
            {
                if (DbObject.GetDataSet(sql, sqlparams, SQLUtility.GetSqlConnString(), "data", out ds, out message))
                {
                    result = DataManager.Bind<List<T>>(ds.Tables[0]);
                }
            }
            catch (Exception ex)
            {
                message = "error message";
            }

            return result;

        }

Solution

  • You need to change the declaration of your CityModel method to return a List<CityModel>:

    //Controller
    namespace StateAPI.Controllers
    {
        public class CityController : ApiController
        {
            SQLConnection SQLconn = new SQLConnection();
    
            public List<CityModel> GetCityInfo(string CityList)
            {
    
                if (string.IsNullOrEmpty(CityList)
                {
                    return new CityModel
                    {
                        Message = "No CityList Passed",
                    };
                }
                else
                {
                    return SQLconn.GetCityMain(CityList);
                }
            }
        }
    }
    

    and you need to add to your List<CityModel> a CityModel but you are adding List<CityModel>:

    public List<CityModel> GetCityMain(string CityList)
    {
        String strSQL = String.Empty;
        String msg = String.Empty;
        String[] parms = { };
    
        List<CityModel> output = new List<CityModel>();
        var cities= CityList.Split(',').Select(x => x.Trim());
    
        foreach (var city in cities)
        {
            strSQL = String.Format(GetCityInfoSQL, city);
            //info will be of type CityModel only then you can add it to output
            List<CityModel> info  = SQLUtil.GetDataList<CityModel>(
            strSQL,
            parms.ToArray<string>(),
            out msg);
    
            output.Add(info.FirstOrDefault()); //Add this to get the current item
        }
        
        return output;
    }