EF 4.0 LINQ one to many / many to one
Just starting to use LINQ as well as EF.
I have a set of tables in t开发者_开发技巧he following configuration:
PublicUtility (UtilityCode) * ----> 1 (UtilityCode) Utility (UtilityCode) 1 -----> * (UtilityCode) UtilityDetail
I have a query in SQL. Based on some other business rules this query will either return 1 value or 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 = @DebtPurposeCodeHow could I rewrite this using LINQ to entities?
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();
}
精彩评论