2013-12-11 33 views
1

我正在访问一个小型Access数据库,并将一些表放入数据集中。然后,我需要用LINQ查询这些表。我与LINQ很新的,我不明白什么是错用下面的代码:无法访问LINQ结果集中的列

DataTable cours = autOuvrDS.Tables["cours"]; 
DataTable etudiants = autOuvrDS.Tables["etudiants"]; 
DataTable resultats = autOuvrDS.Tables["resultats"]; 

IEnumerable<DataRow> query = from etudiant in etudiants.AsEnumerable() 
          join resultat in resultats.AsEnumerable() 
          on etudiant.Field<string>("matricule") equals resultat.Field<string>("matricule") 
          join cour in cours.AsEnumerable() 
          on resultat.Field<string>("sigle") equals cour.Field<string>("sigle") 
          select etudiant; 

DataTable table = query.CopyToDataTable<DataRow>(); 

然后我循环通过每一行打印数据:

foreach (DataRow row in table.Rows) 
     { 
      Console.WriteLine(
       row.Field<string>("Prenom") + "\t\t" + 
       row.Field<string>("Nom") + "\t\t" + 
       row.Field<string>("Sigle") + "\t\t" + /// PROGRAM CRASH HERE 
       row.Field<string>("Cours") + "\t\t" 
       ); 
     } 

,并获得以下执行程序时出错:“附加信息:Column'Sigle'不属于表格。”

列“Sigle”是“cours”表的一部分,我已经加入了查询,所以我会选择查询来选择连接表中的列,但似乎并非如此..那么我怎样才能从其他表中选择列?

+0

只需将PROGRAM CRASH HERE中的berakpoint,并检查您是否访问错误的列名称或可能转换为错误的数据类型。 – YOusaFZai

回答

0

您的查询将加入三组DataRow对象,然后选择其中一个DataRow对象作为输出。生成的table对象包含仅源于etudiants源表的行。

如果你想只有你列出的字段,你可以声明结构持有类似这样的结果:

public struct QueryResult 
{ 
    public string Prenom; 
    public string Nom; 
    public string Sigle; 
    public string Cours; 
} 

然后,当你想运行查询:

var query = 
    from etudiant in etudiants.AsEnumerable() 
    join resultat in resultats.AsEnumerable() 
    on etudiant.Field<string>("matricule") equals resultat.Field<string>("matricule") 
    join cour in cours.AsEnumerable() 
    on resultat.Field<string>("sigle") equals cour.Field<string>("sigle") 
    select new QueryResult 
    { 
     Prenom = etudiant.Field<string>("Prenom"), 
     Nom = etudiant.Field<string>("Nom"), 
     Sigle = resultat.Field<string>("Sigle"), 
     Cours = resultat.Field<string>("Cours") 
    }; 

List<QueryResult> results = query.ToList(); 

foreach (QueryResult row in results) 
{ 
    Console.WriteLine("{0}\t\t{1}\t\t{2}\t\t{3}\t\t", 
     row.Prenom, row.Nom, row.Sigle, row.Cours 
    ); 
} 

你可以使用匿名类型,但只能在创建实例的方法之外的任何地方使用结果。相信我,除非结果完全在单一方法内部,否则声明结构更简单。

这将做同样的查询,并产生充满选择字段值从结果DataTable


DataTable执行更新

var query = 
    from etudiant in etudiants.AsEnumerable() 
    join resultat in resultats.AsEnumerable() 
    on etudiant.Field<string>("matricule") equals resultat.Field<string>("matricule") 
    join cour in cours.AsEnumerable() 
    on resultat.Field<string>("sigle") equals cour.Field<string>("sigle") 
    select new { etudiant, cour }; 


// Définir la structure de la table de sortie 
DataTable table = new DataTable(); 
table.Columns.Add("Prenom", typeof(string)); 
table.Columns.Add("Nom", typeof(string)); 
table.Columns.Add("Sigle", typeof(string)); 
table.Columns.Add("Cours", typeof(string)); 

// Remplir la table de sortie à partir la requête 
foreach (var row in query) 
{ 
    DataRow newrow = table.NewRow(); 
    newrow["Prenom"] = row.etudiant.Field<string>("Prenom"); 
    newrow["Nom"] = row.etudiant.Field<string>("Nom"); 
    newrow["Sigle"] = row.cour.Field<string>("Sigle"); 
    newrow["Cours"] = row.cour.Field<string>("Cours"); 
    table.Rows.Add(newrow); 
} 

虽然有可能是一个更有效的方式来实现这一点,无论是在代码和实际运行时间,事实上,你不得不使用DataTable而不是更多的以.NET为中心的形式这是我通常不会面对的事情。

+0

这是工作,但我需要将结果集放入DataTable中。我尝试将查询切换回“IEnumerable ”而不是“var”,但是随后在第二次连接时出现错误:“无法将类型'System.Collections.Generic.IEnumerable <>'隐式转换为'System.Collections .Generic.IEnumerable '。显式转换存在(你是否缺少一个转换?)“ – SimCity

+0

为什么你必须有一个'DataTable'?这个DataTable具有什么格式? – Corey

+0

实际上,这是一个家庭作业问题的一部分,它要求将数据集放置在DataTable中,所以除了将它放在表格中之外没有其他真正重要的原因。我不太确定,你对表的格式? – SimCity

1

您写道Sigle属于cours表,但在您的LINQ查询中,您只从etudiants表中选择数据:select etudiant。所以这个列不能在你的输出中。仔细查看你的代码。