2013-12-11 34 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。所以這個列不能在你的輸出中。仔細查看你的代碼。