2011-05-26 58 views
2

我有以下兩個LINQ查詢:如何逐步構建LINQ => SQL /實體查詢(帶連接)?

public int getJobsCount() 
{ 
    var numJobs = 
     (from j in dbConnection.jobs 
     join i in dbConnection.industries on j.industryId equals i.id 
     join c in dbConnection.cities on j.cityId equals c.id 
     join s in dbConnection.states on j.stateId equals s.id 
     join pt in dbConnection.positionTypes on j.positionTypeId equals pt.id 
     select j).Count(); 
    return numJobs; 
} 

public List<Job> getJobs() 
{ 
    var jobs = 
     (
      from j in dbConnection.jobs 
      join i in dbConnection.industries on j.industryId equals i.id 
      join c in dbConnection.cities on j.cityId equals c.id 
      join s in dbConnection.states on j.stateId equals s.id 
      join pt in dbConnection.positionTypes on j.positionTypeId equals pt.id 
      orderby j.issueDatetime descending 
      select new Job { x = j.field, y = c.field, etc } 
     ).Skip(startJob - 1).Take(numJobs); 
    return jobs; 
} 

有很多的在那裏重複的代碼 - 「從」和「加盟」線是相同的,我會在一些「其中」被添加線也是一樣的。

我想補充說,返回一個IQueryable對於第一部分的方法:

public IQueryable getJobsQuery() 
{ 
    var q = 
     from j in dbConnection.jobs 
     join i in dbConnection.industries on j.industryId equals i.id 
     join c in dbConnection.cities on j.cityId equals c.id 
     join s in dbConnection.states on j.stateId equals s.id 
     join pt in dbConnection.positionTypes on j.positionTypeId equals pt.id; 
    return q; 
} 

...但我得到「查詢體必須與SELECT子句或group子句結束」。

如果我堅持到最後關閉該功能添加一個選擇條款,我不能調用的結果數():

// getJobsQuery: 
var q = from j in dbConnection.jobs 
     join i in dbConnection.industries on j.industryId equals i.id 
     join c in dbConnection.cities on j.cityId equals c.id 
     join s in dbConnection.states on j.stateId equals s.id 
     join pt in dbConnection.positionTypes on j.positionTypeId equals pt.id 
     select new { a = j.y, b = c.z } 

// another method: 
var q = getJobsQuery(); 
var numJobs = q.Count(); // "IQueryable doesn't contain a definition for count" 

是否有建立這樣的查詢採取逐步的方式避免重複一大堆代碼的步驟?

+0

哦,你不能使用計數可能導致的原因你缺乏System.Linq的命名空間包含。 – Alxandr 2011-05-26 07:10:56

回答

3

有寫LINQ查詢兩種方式,雖然它並不重要女巫一個使用它的好,原因知道他們兩個,他們可能會學到一些關於LINQ如何工作的信息。

例如,您有一組作業。如果你選擇用5的industryId(數據類型的胡亂猜測)所有的工作你可能會寫這樣的事:

from j in dbConnection.jobs 
where j.inustryId == 5 
select j; 

非常相同的查詢也可以寫成這樣

dbConnections.jobs.Where(j => j.industryId == 5); 

現在,我不是在這裏宣揚一種方式比另一種更好,但在這裏你可以清楚地看到LINQ如何使用擴展方法語法自動選擇迭代對象(除非你做了選擇),而在查詢語法中,你必須明確地做到這一點。另外,如果你要添加的旅館這裏另一個where子句會是這個樣子:

from j in dbConnection.jobs 
where j.inustryId == 5 // not using && here just to prove a point 
where j.cityId == 3 // I THINK this is valid syntax, I don't really use the query-syntax in linq 
select j; 

雖然在擴展的方法你可以追加更多的方法,調用像這樣:

dbConnections.jobs.Where(j => j.industryId == 5) 
    .Where(j => j.cityId == 3); 

現在,這是很好的知道原因,這意味着你可以將你的linq查詢放入函數中繼續查詢。和所有你需要做的,讓你的情況的工作僅僅是明確地選擇起始變量j,或者你需要像這樣所有的變量:

var q = 
    from j in dbConnection.jobs 
    join i in dbConnection.industries on j.industryId equals i.id 
    join c in dbConnection.cities on j.cityId equals c.id 
    join s in dbConnection.states on j.stateId equals s.id 
    join pt in dbConnection.positionTypes on j.positionTypeId equals pt.id; 
      select new {j = j, i = i, c = c, s = s, pt = pt }; 
return q; 

那麼你應該能夠例如該做的事:

getJobsQuery().Where(a => a.i.id == 5); // I used a as a name for "all", like the collection of variables 

或使用查詢語法

from a in getJobsQuery() 
where a.i.id == 5 
select a; 
+0

你有額外的;在選擇新的{j = j,...'之前。否則,好東西。 – 2011-05-26 17:44:38

+0

感謝Alxandr,那是一種享受。 :) – George 2011-05-27 05:21:26

0

通過返回一組數據(例如公共數據)並查詢該數據的子集可以更好地解決這個問題嗎?

E.g. [僞]

var allJobs = 
    (from j in dbConnection.jobs 
    join i in dbConnection.industries on j.industryId equals i.id 
    join c in dbConnection.cities on j.cityId equals c.id 
    join s in dbConnection.states on j.stateId equals s.id 
    join pt in dbConnection.positionTypes on j.positionTypeId equals pt.id 
    select j); 

var myJobs = allJobs.OrderBy(j => j.issuedate).skip(expr).Take(allJobs.Count); 

或類似...

+0

後來我想要在「城市」以外的表中運行一些「where」子句 - 而且如果我的第一個查詢只選擇「jobs」,看起來並不像我能做到的那樣「 記錄? – George 2011-05-26 07:08:32