我的WCF服务实现中有这个代码,我收到此错误 :
'an object reference is required to access non-static field, method or property
System.Data.Linq.DataContext.User.get
如何正确更新用户的位置?这是代码:
public void UpdateLocation(string usrName, double usrLong, double usrLat)
{
var result = (from usr in DataClasses1DataContext.Users
where usr.usrName == usrName
select usr).Single();
result.usrLong = usrLong;
result.usrLat = usrLat;
DataClasses1DataContext.SubmitChanges();}
答案 0 :(得分:2)
错误消息告诉您需要知道的所有内容,Users
属性不是静态的,而是按原来的意思调用它。您需要一个DataClasses1DataContext
的实例来调用该属性:
using(DataClasses1DataContext context = new DataClasses1DataContext())
{
var result = (from usr in context.Users
where usr.usrName == usrName
select usr).Single();
}
答案 1 :(得分:0)
正如伊恩所说,
public void UpdateLocation(string usrName, double usrLong, double usrLat)
{
using(DataClasses1DataContext Usercontext = new DataClasses1DataContext())
{
var result = (from usr in Usercontext.Users where usr.usrName == usrName select usr).Single();
result.usrLong = usrLong;
result.usrLat = usrLat;
Usercontext.SubmitChanges();
}
}