Question

Just starting to use LINQ as well as EF.

I have a set of tables in the 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 = @DebtPurposeCode

How could I rewrite this using LINQ to entities?

Was it helpful?

Solution

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top