2012-11-12 35 views
4

我正在收集來自兩個不同源的數據,目的是填充單個對象。我的班級將來自不同源的兩個LINQ查詢合併爲單個對象

public class SampleClient 
{ 
    public string ClientId { get; set; } 
    public string JobName { get; set; } 
    public string JobUrl { get; set; } 
    public string Version { get; set; } 
} 

第一LINQ查詢填充SampleClient對象獲得的所有版本除外值的集合。然後我需要遍歷SampleClient的集合,並在第二個查詢中使用ClientId值。第二個查詢的結果是版本值。這是我的第一個LINQ查詢:

private void btnProjects_Click(object sender, EventArgs e) 
    { 
     string[] stringArray = { "demo", "sample", "test", "training" }; 
     List<SampleClient> jobList = 
     (
      from x in XDocument.Load(XML URL VALUE HERE).Root.Elements("job") 
      where x.Element("name").Value.Contains("-config") && x.Element("name").Value.StartsWith("oba-") 
      && (!stringArray.Any(s => x.Element("name").Value.Contains(s))) 
      select new SampleClient 
      { 
       ClientId = getClientId((string)x.Element("name")), 
       JobName = (string)x.Element("name"), 
       JobUrl = (string)x.Element("url"), 
      }).ToList(); 

     foreach (SampleClient job in jobList) 
     { 
      //Display the results in a textbox 
      txtResults.Text += "Job Name = " + job.JobName + " | Job URL = " + job.JobUrl + " | "; 
     } 
    } 

在其取得上述版本的查詢,我目前有:

var version = from item in doc.Descendants(ns + "properties") 
select new SampleClient 
{ 
    ClientId = strClientId, 
    Version = (string)item.Element(ns + "product.version").Value 
}; 

需要注意的是在第二查詢中使用的URL源創建使用是非常重要的從第一個獲得的ClientId。我可以通過一個例程循環來清理/合併對象,但這感覺像是一種黑客攻擊。有沒有更好的方法來處理這個問題?

這是從第一查詢的XML示例:

<?xml version="1.0"?> 
<allView> 
    <description>PROD</description> 
    <job> 
     <name>abc</name> 
     <url>http://ci-client.company.net/job/abc/</url> 
     <color>blue</color> 
    </job> 
    <job> 
     <name>acme</name> 
     <url>http://ci-client.company.net/job/acme/</url> 
     <color>blue</color> 
    </job> 
</allView> 

第二個查詢URL被動態地通過使用clientID的從第一的結果創建的。我使用它來加載Maven POM文件並獲取版本信息。這是來自POM文件的XML片段。

<?xml version="1.0" encoding="UTF-8" ?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.company.xyz</groupId> 
    <artifactId>io</artifactId> 
    <version>6.11.7-ACME-SNAPSHOT</version> 
    <packaging>pom</packaging> 
    <properties> 
     <product.version>6.11.7</product.version> 
     <db.version>1.4</db.version> 
    </properties> 
    <modules> 
     <module>policies</module> 
     <module>templates</module> 
     <module>assemble</module> 
    </modules> 
</project> 
+0

在哪裏呢strClientId來自於第二個查詢? –

+0

意圖(雖然功能尚未完善)是它會來自循環第一個查詢的結果。換言之,它是由第一個查詢創建的SampleClient集合中的ClientId。我希望這是有道理的。 –

+0

顯示您的XML源代碼示例。 – dtb

回答

0

試試這個:

var jobList = from x in XDocument.Load(... 
       where ... 
       let clientId = getClientId((string)x.Element("name")) 
       select new SampleClient 
       { 
        ClientId = clientId, 
        JobName = (string)x.Element("name"), 
        JobUrl = (string)x.Element("url"), 
        Version = (string)XDocument 
         .Load(GetSecondQueryUrl(clientId)) 
         .Root 
         .Element(pom4 + "properties") 
         .Element(pom4 + "product.version") 
       }; 
+0

謝謝dtb。我會在早上嘗試。假設這有效,它肯定比我想像的要乾淨。我真的需要學習這個LINQ的東西! –

+0

這工作完美。再次感謝你的幫助。 –

2

我希望我明白你的問題..

這將合併在一起(雖然,我的測試數據只有客戶端Id,作業名和版本..)。查詢1包含ClientId和JobName,Query2包含ClientId和版本。

IList<SampleClient> mergedList = firstQuery.Concat(secondQuery) 
    .ToLookup(x => x.ClientId) 
    .Select(x => x.Aggregate((query1, query2) => 
      new SampleClient() { 
        ClientId = query1.ClientId, 
        JobName = query1.JobName, 
        Version = query2.Version 
       } 
      ) 
    ).ToList(); 

..possibly效率不高,您的手動方法。但是我沒有什麼基準對..

+0

感謝您的快速反饋。我認爲你明白我的問題,我會在早上嘗試。由於我是LINQ新手,我不得不問一個可能是一個愚蠢的問題。您引用firstQuery,secondQuery以及query1和query2。你能幫我把這些定義在一起嗎?再次感謝。 –

+0

'firstQuery'和'secondQuery'只是您在問題中提到的兩個查詢的佔位符。我使用虛擬數據預填充我的佔位符。 query1和query2是聚集用於構建返回列表的單個項目。 –

相關問題