2011-01-11 91 views
0

剛開始使用LINQ以及EF。EF 4.0 LINQ一對多/多對一

我有以下配置的一組表:

PublicUtility(UtilityCode)* ----> 1(UtilityCode)實用工具(UtilityCode)1 - - - - > *(UtilityCode)UtilityDetail

我在SQL中有一個查詢。根據其他業務規則,此查詢將返回1值或NULL。

SELECT
@UtilityCode = UtilityDetail.UtilityCode
FROM
UtilityDetail
INNER JOIN PublicUtility ON
PublicUtility.SubdivisionCode = @SubdivisionCode AND
PublicUtility.Year = @PublicUtilityYear AND
PublicUtility.UtilityCode = UtilityDetail .UtilityCode
WHERE
UtilityDetail.DebtPurposeCode = @DebtPurposeCode

我怎麼能重寫這個使用LINQ到實體?

回答

0
using (YourObjectContext ctx = new YourObjectContext()) 
{ 
    var code = (from ud in ctx.UtilityDetails 
       join pu in PublicUtility on ud.UtilityCode equals pu.UtilityCode 
       where ud.DeptPurposeCode == [code_value] && pu.SubdivisionCode == [subdivcode_value] && pu.Year == [year_value] 
       select new {ud.UtilityCode}).FirstOrDefault(); 
} 
+0

謝謝。這讓我瘋狂。我試圖從PublicUtility.Utility.UtilityDetails中引用UtilityDetails,但沒有獲得任何地方。我無法在T-SQL中思考問題... – 2011-01-11 18:29:34