0

我有一个名为 Company 的表,我正在尝试使用 linq Query 访问该表的数据并转换为公司 Calss

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>();

但它给出了像 ToList() 这样的错误有一些无效的参数如何给列名获取列表格式的数据

提前谢谢

4

4 回答 4

3

我想你正在寻找这个:

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(); 

换句话说,您的代码正在实例化一个匿名类型,但您正在尝试创建一个 Company 对象列表。因此,请实例化一个 Company 对象。

但是,您可能不需要创建一组新的 Company 对象,其属性从另一组 Company 对象中复制而来。如果这是真的,你可以这样做:

List<Company> companyData = dataContext.Companies.ToList();

如果您需要以不同的方式命名属性,那么使用匿名类型是正确的。在这种情况下,您必须使用var关键字,因为对象不再是Company类的实例:

 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();  
于 2012-04-13T04:51:07.403 回答
1
List<Company> companyData = (from c in dataContext.Companies 
           select new Company() { ... }).ToList();
于 2012-04-13T04:50:02.557 回答
1

如果你的意思是DataTable,那么你可以这样做:

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; }
}
于 2012-04-13T04:54:44.667 回答
0
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();
于 2012-04-13T11:23:02.560 回答