1

I've just started learning Linq, and I'm trying to map a database table to a class, but I can't get it to work with a private constructor.

My code:

namespace LinqTest
{
    using System.Collections.Generic;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Linq;

    [Table(Name = "tblCity")]
    public class City
    {
        private City()
        {
        }

        [Column(IsPrimaryKey = true, Name = "CityId")]
        public int Id { get; private set; }

        [Column(Name = "CityName")]
        public string Name { get; private set; }

        public static List<City> GetList()
        {
            var dataContext = new DataContext(Database.ConnectionString());
            var cities = dataContext.GetTable<City>();

            var iQueryable =
                from city in cities
                select city;

            return iQueryable.ToList();
        }
    }
}

I've also tried mapping to a new instance:

var list = new List<City>();

            foreach (var iCity in iQueryable)
            {

                var city = new City
                {
                    Id = iCity.Id,
                    Name = iCity.Name
                };

                list.Add(city);
            }

            return list;

What can I do to make this work? I'm open to alternative methods. Thanks

user2078938
  • 947
  • 1
  • 9
  • 22

1 Answers1

1

You cant use iqueryable object in foreach. Because this is just a query. You must get objects with ToList(). Pls try this:

var list = new List<City>();

            foreach (var iCity in iQueryable.ToList())// return iqueryable to list
            {

                var city = new City
                {
                    Id = iCity.Id,
                    Name = iCity.Name
                };

                list.Add(city);
            }

            return list;

Or You can search automapper. One example here

Community
  • 1
  • 1
  • Thanks, this is a huge help. Is there a way I can avoid the second mapping in the foreach loop? – user2078938 Feb 10 '16 at 12:01
  • Be more clear please. Avoid second mapping is mean duplicate city? – Alper Tunga Arslan Feb 10 '16 at 12:04
  • If you need to avoid duplicate look this [example](http://stackoverflow.com/questions/1606679/remove-duplicates-in-the-list-using-linq) . – Alper Tunga Arslan Feb 10 '16 at 12:13
  • The database table and columns are mapped to the Class and properties respectively. This mapping is coded again in the city constructor in the foreach loop. Is there a way to reduce this duplicity? – user2078938 Feb 10 '16 at 12:16
  • Sorry i dont know anything about this topic. I think this question solved. Maybe you need to ask new question about this topic. Have a good day. – Alper Tunga Arslan Feb 10 '16 at 12:23
  • While this worked, my 'Select' statement from iQueryable had to be: select new { city.Id, city.Name }; – user2078938 Feb 10 '16 at 13:03
  • yes you can use select new { city.Id, city.Name }; but if you want use iCity.Id you must be create anonmous type. select new newAnonymoustype { city.Id, city.Name }; just create a new type and use like this. – Alper Tunga Arslan Feb 10 '16 at 13:29