LINQ Range Variable Problem

Posted by: Steven Smith, on 05 Oct 2010 | View original | Bookmarked: 0 time(s)

Ran into this issue last night and just figured it out.  I have this code for a demo:

using System;
using System.Data.Objects;
using System.Transactions;
using ExtractTransformLoad.Domain;
using Northwind.Entities;
//using System.Linq;

namespace ExtractTransformLoad
{
public class SqlFreightByShipperRepository : IFreightByShipperRepository
{
public void DeleteAndInsert(DateTime runDate, FreightByShipper freightByShipper)
{
using (var context = new NorthwindEntities())
using (var scope = new TransactionScope())
{
var summaryToDelete = (from freightSummary in context.FreightSummaries
where
freightSummary.RunDate == runDate &&
freightSummary.ShipperName == freightByShipper.ShipperName

select freightSummary);//.FirstOrDefault();
context.DeleteObject(summaryToDelete);

var newFreightSummary = new FreightSummary()
{
Freight = freightByShipper.Freight,
RunDate = DateTime.Today,
ShipperName = freightByShipper.ShipperName
};
context.FreightSummaries.AddObject(newFreightSummary);
context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
scope.Complete();
}
}
}
}

Now, in Visual Studio, it looks like this:

image

Hovering over freightSummary in the above LINQ query would reveal just

(range variable) ? freightSummary

image

A bunch of searching later didnt much help, although this thread did put me on the right track just a moment ago.  The trick, it turns out, is that I didnt have the System.Linq namespace included (you can see above I just re-commented it out).  If you include System.Linq, everything works as expected.  Final code here:

using System;
using System.Data.Objects;
using System.Transactions;
using ExtractTransformLoad.Domain;
using Northwind.Entities;
using System.Linq;

namespace ExtractTransformLoad
{
public class SqlFreightByShipperRepository : IFreightByShipperRepository
{
public void DeleteAndInsert(DateTime runDate, FreightByShipper freightByShipper)
{
using (var context = new NorthwindEntities())
using (var scope = new TransactionScope())
{
var summaryToDelete = (from freightSummary in context.FreightSummaries
where
freightSummary.RunDate == runDate &&
freightSummary.ShipperName == freightByShipper.ShipperName

select freightSummary).FirstOrDefault();
context.DeleteObject(summaryToDelete);

var newFreightSummary = new FreightSummary()
{
Freight = freightByShipper.Freight,
RunDate = DateTime.Today,
ShipperName = freightByShipper.ShipperName
};
context.FreightSummaries.AddObject(newFreightSummary);
context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
scope.Complete();
}
}
}
}

I guess Im spoiled by Resharper, which normally prompts me for any namespace it detects that Im trying to use but havent yet included.  It wasnt until I added the .FirstOrDefault() that R# kicked in, and then for some reason it couldnt automatically add the using statement for System.Linq, but once I did so by hand, the Range Variable problem disappeared.


Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: XLinq | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 874 | Hits: 7

Similar Posts

  • VBA code I wrote this week in Excel and how it helped me more
  • LINQ to SQL, Lazy Loading and Prefetching more
  • Session State Not Working? Check Your Web Garden! more
  • System.Data.Linq.Binary is not XmlSerializable more
  • Linq: Beware of the 'Access to modified closure' demon more
  • LINQ in Action samples in LINQPad more
  • How Fast are In Memory LINQ Evaluations for Doing Simple Things? more
  • Iterating on an ASP.NET MVC Model Binder more
  • YAPES: Problem Ten more
  • YAPES: Problem Eight more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD