以下代码抛出NullReferenceException.如果我使用另一列过滤用户表,则不会抛出异常.DateTime类型列会导致此异常.
using (MyDataContext dc = new MyDataContext())
{
var ids = dc.Users.Where(c => c.BirthDate < DateTime.Now.AddYears(-10)).Select(c => c.UserId);
var list = (from c in dc.Users
where ids.Contains(c.UserId)
select c).ToList();
}
Run Code Online (Sandbox Code Playgroud)
像这样的堆栈跟踪:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Data.Linq.SqlClient.SqlFactory.Member(SqlExpression expr, MemberInfo member) +182
System.Data.Linq.SqlClient.QueryConverter.VisitMemberAccess(MemberExpression ma) +260
System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +939
System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node) +18
System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +18
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc) +1011
System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node) +1003
System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node) +18
System.Data.Linq.SqlClient.QueryConverter.VisitExpression(Expression exp) +18
System.Data.Linq.SqlClient.QueryConverter.VisitChangeType(Expression expression, Type type) +12
System.Data.Linq.SqlClient.QueryConverter.VisitCast(UnaryExpression c) +117
System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression …Run Code Online (Sandbox Code Playgroud) 我有查询:
var q = db.tblArcadeGamePlays
.Where(c => c.GameID == GameID && c.ReferalID != 0 && c.ReferalID != null)
.GroupBy(r => new { r.ReferalID })
.Select(g => new { Plays = g.Count(), URL = g.Key.ReferalID })
;
Run Code Online (Sandbox Code Playgroud)
tblArcadeGamePlays还有一个字段Date,在该组中我想返回该组包含的最早观察日期.
我该怎么做?
我查询数据库并使用以下两种方法将结果推送到控制台和文件:
var result = from p in _db.Pages
join r in rank on p.PkId equals r.Key
orderby r.NumPages descending
select new KeyNameCount
{
PageID = p.PkId,
PageName = p.Name,
Count = r.NumPages
};
WriteFindingsToFile(result, parentId);
WriteFindingsToConsole(result, parentId);
Run Code Online (Sandbox Code Playgroud)
当用作两个方法调用中的参数时,IEnumerable <T>,而不是IQuerable <T>用作结果的参数类型.
在两次调用中,结果都在foreach中迭代.这导致针对数据库的两个相同的调用,每个方法一个.
我可以将这两个方法重构为一个并且只使用一个foreach,但这很快就会变得非常难以维护(添加写入html,写入xml等)
我很确定这是一个非常常见的问题,但使用谷歌并没有让我更聪明,所以我转向你们:-)
我有一个Linq2SQL上下文,有2个表,一个用于Users,一个用于Posts.最初我有这个查询:
var results = db.Users.Select(m => new UserModel
{
Id = m.Id,
DisplayName = m.DisplayName,
PostCount = m.Posts.Count(),
}).ToList();
Run Code Online (Sandbox Code Playgroud)
这很有效,没有n + 1.然后我决定编写第二个以不同方式过滤用户的函数,并认为我会聪明并将select移动到a Func<User, UserModel>然后从我的两个查询中调用它.此查询特别更改为如下所示:
Func<User, UserModel> UserModelSelector =
m => new UserModel
{
Id = m.Id,
DisplayName = m.DisplayName,
PostCount = m.Posts.Count(),
};
var results = db.Users.Select(UserModelSelector).ToList();
Run Code Online (Sandbox Code Playgroud)
只有现在这个查询通过加载每个User单独的帖子计数得到n + 1 .我已经尝试过将其设置为public/private/internal,readonly,static的所有组合,并且在所有情况下都会出现n + 1.
任何人都可以解释这里发生了什么?
使用linq to sql我想在数据上下文中生成一些密封的类.显然,这是无法做到的.
当我尝试扩展生成的类时,让我们说这样ApplicationUser的部分部分
sealed public partial class ApplicationUser
{ }
Run Code Online (Sandbox Code Playgroud)
我收到编译错误(总结)
'SendPropertyChanging()'是密封类'ApplicationUser'中的新虚拟成员
和
'SendPropertyChanged(string)'是密封类'ApplicationUser'中的新虚拟成员
显然因为这些方法是生成的protected virtual.
有没有办法让linq到sql生成选定(不是全部)类密封?
我需要使用过滤器列表过滤linq查询,我计划使用contains方法来执行此操作.所以它看起来像这样.
List<string> filter = new List<string>();
filter.Add("foo");
filter.Add("bar");
//Additional filters go here, max of about 10 filters
var test = dbcontext.books.Where(x => filter.Contains(x.name)).ToList();
Run Code Online (Sandbox Code Playgroud)
此查询后面的表有很多记录(500,000),PK标识字段和我要查询的字段上的索引.
我的问题是在走这条路线之前您是否希望此查询的性能可以接受,或者我应该在这么大的数据集上使用不同的方法?
我正在开发一个WP7芒果应用程序,它利用Linq2SQL进行数据访问.我有一个Note实体,它有一个类型为int的自动生成键.
我第一次向数据库添加新注释时,操作正常,注释保存,然后如果我从数据库中删除它,它也会从数据库中删除.第一个实体始终为Id = 0.
然后,如果我想在删除第一个音符后添加新音符,我会得到一个例外,表明该实体已经存在.我得出结论,即使我在数据上下文中调用SubmitChanges,也没有删除Id = 0的第一个实体.
此外,我在我的存储库和相同的存储库实例(出于性能原因的单例)上使用相同的数据上下文进行数据操作.为了确认这种行为,我试图连续调用,但它失败了!
this.DbContext.Notes.DeleteOnSubmit(value);
this.DbContext.SubmitChanges();
this.DbContext.Notes.InsertOnSubmit(value);
this.DbContext.SubmitChanges();
Run Code Online (Sandbox Code Playgroud)
它表示它无法添加已存在的实体.对此行为有何解释?提前致谢.
注意:当我使用数据上下文的两个不同实例时,此行为将消失.
我从朋友那里得到了以下代码的建议
var tmp = from _db.tblCourseNeededHours中的allCourses orderby allCourses.tblCourse.CourseName其中allCourses.Semester == semester选择allCourses;
return tmp.Sum(x => x.NeededHoursPerWeek);
但我接受了这个错误
无法将类型'double'隐式转换为'System.Linq.IQueryable
知道怎么解决吗?

public IQueryable FindSumAllCourseNeededHoursInSemester(string semester)
我不明白为什么这个查询中的"dbo"
var searchUser = from user in dbo.Accounts
where user.accnt_user == txtUser.Text &&
user.accnt_pass == txtPassword.Text
select user;
Run Code Online (Sandbox Code Playgroud)
显示此错误
"当前上下文中不存在名称'dbo'"
但当我删除"dbo"这个词时,它会向我显示这个错误
名称帐户在当前上下文中不存在.
这是我的表 
在我的DataClasssesContext中,我有这个
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Accounts")]
public partial class Account : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _accnt_ID;
private string _accnt_User;
private string _accnt_Pass;
private string _accnt_Position;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void Onaccnt_IDChanging(int value);
partial void Onaccnt_IDChanged();
partial void Onaccnt_UserChanging(string value);
partial void Onaccnt_UserChanged(); …Run Code Online (Sandbox Code Playgroud) 我已经对密码进行了哈希,然后我将其插入到数据库中,但问题是每次我尝试执行此查询时都会发生这种情况
Cannot implicitly convert type 'string' to 'System.Data.Linq.Binary'
Run Code Online (Sandbox Code Playgroud)
在我的数据库表中,accnt_pass是Binary(50),这是我的代码
//Instantiate a new Hasher Object
var hasher = new Hasher();
hasher.SaltSize = 16;
//Encrypts The password
var encryptedPassword = hasher.Encrypt(txtPass.Text);
Account newUser = new Account();
newUser.accnt_User = txtUser.Text;
newUser.accnt_Position = txtPosition.Text;
newUser.accnt_Pass = encryptedPassword;
Run Code Online (Sandbox Code Playgroud)
我正在使用Encrypto进行散列,
c# ×10
linq-to-sql ×10
linq ×3
.net ×2
asp.net ×1
database ×1
datacontext ×1
hash ×1
silverlight ×1