Reputation: 89
I have One table named Company in this i am trying to access the data of that table using linq Query and converted into company Calss like
List<Company> companyData = (from c in dataContext.Companies
select new
{
CompanyName =c.CompanyName,
Address1 =c.Address1,
Addredd2 =c.Address2,
city=c.City.CityName,
state =c.State.StateName,
country =c.Country.CountryName,
Telephone1 =c.Telephone1,
Telephone2 =c.Telephone2,
Mobile1 =c.Mobile1,
Mobile2=c.Mobile2,
Email1 =c.Email1,
Email2 = c.Mobile2,
Fax1=c.Fax1,
Fax2=c.Fax2,
TinNo=c.TinNo,
IsGroupCompany=c.IsGroupCompany
}).ToList<Company>();
but it gives error like ToList() have some invalid argument how can in give the columnname get data in List format
Thaks in Advance
Upvotes: 0
Views: 1205
Reputation: 173
try this :-
List<Company> companyData = (from c in dataContext.Companies
select new Company
{
CompanyName =c.CompanyName,
Address1 =c.Address1,
Addredd2 =c.Address2,
city=c.City.CityName,
state =c.State.StateName,
country =c.Country.CountryName,
Telephone1 =c.Telephone1,
Telephone2 =c.Telephone2,
Mobile1 =c.Mobile1,
Mobile2=c.Mobile2,
Email1 =c.Email1,
Email2 = c.Mobile2,
Fax1=c.Fax1,
Fax2=c.Fax2,
TinNo=c.TinNo,
IsGroupCompany=c.IsGroupCompany
}).ToList();
Upvotes: 0
Reputation: 43036
I think you're looking for this:
List<Company> companyData = (from c in dataContext.Companies select new Company {
CompanyName = c.CompanyName,
Address1 = c.Address1,
Address2 = c.Address2,
City = c.City,
State = c.State,
Country = c.Country,
Telephone1 = c.Telephone1,
Telephone2 = c.Telephone2,
Mobile1 = c.Mobile1,
Mobile2 = c.Mobile2,
Email1 = c.Email1,
Email2 = c.Email2,
Fax1 = c.Fax1,
Fax2 = c.Fax2,
TinNo = c.TinNo,
IsGroupCompany = c.IsGroupCompany }
).ToList();
In other words, your code is instantiating an anonymous type, but you are trying to create a list of Company objects. Therefore, instantiate a Company object instead.
Chances are, however, that you don't need to create a set of new Company objects with their properties copied from each of another set of Company objects. If that's true, you can just do this:
List<Company> companyData = dataContext.Companies.ToList();
If you need to name the properties differently, then you are correct to use an anonymous type. In that case, you must use the var
keyword, since the objects are no longer instances of the Company
class:
var companyData = (from c in dataContext.Companies
select new
{
CompanyName =c.CompanyName,
Address1 =c.Address1,
Addredd2 =c.Address2,
city=c.City.CityName,
state =c.State.StateName,
country =c.Country.CountryName,
Telephone1 =c.Telephone1,
Telephone2 =c.Telephone2,
Mobile1 =c.Mobile1,
Mobile2=c.Mobile2,
Email1 =c.Email1,
Email2 = c.Mobile2,
Fax1=c.Fax1,
Fax2=c.Fax2,
TinNo=c.TinNo,
IsGroupCompany=c.IsGroupCompany
}).ToList();
Upvotes: 3
Reputation: 4546
If you mean to DataTable, then you can do:
class Program
{
static void Main(string[] args)
{
DataTable table = new DataTable();
table.Columns.Add("name", typeof(string));
table.Columns.Add("address", typeof(string));
table.Rows.Add("name 1", "address 1");
table.Rows.Add("name 1", "address 1");
var query = table.AsEnumerable().Select(s => new Company { Name = (string)s["name"], Address = (string)s["address"] }).ToList();
}
}
class Company
{
public string Name { get; set; }
public string Address { get; set; }
}
Upvotes: 1
Reputation: 12776
List<Company> companyData = (from c in dataContext.Companies
select new Company() { ... }).ToList();
Upvotes: 1