ASP.NET: How to test linQ query

How to test a LINQ query, as follows:

var vUser = (from u in this.dbName.aspnet_Users< br /> where u.UserName.Equals(this.wCreateUser.UserName)
select u).Single();

I keep getting the null exception of vUser, but I am pretty sure for such a user Exist.

The record must exist or an exception must be thrown during the evaluation of the lambda, otherwise an exception will be thrown Will be

The sequence contains no elements.

You are accessing any of the vUser objects after the query Attribute, or may this.wCreateUser object be null?

Edit: Comment..

If the exception is that the sequence does not contain elements, the generated query does not return results. I suggest you check the generated query and test it directly against SQL Server.

You can do this in several ways.

>Open SQL Profiler and watch
the query being executed.
>Attach TextWriter to DataContext.Log So that you can see the output of the query
(e.g.
below)..
>use LINQPad as the suggested other answer.

.

p>

StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
this.dcLAUNCHOnline.Log = writer;
//Execute Query..
//sb.ToString(); //will contain the sql produced by the LINQ Query

How to test a LINQ query as follows:

var vUser = (from u in this.dbName.aspnet_Users
where u.UserName.Equals(this.wCreateUser.UserName)
select u).Single ();

I keep getting the null exception of vUser, but I am pretty sure that such a user exists.

The record must exist or be in An exception must be thrown during lambda evaluation, otherwise the thrown exception will be

The sequence contains no elements.

< p>Are you accessing any properties on the vUser object after the query, or the this.wCreateUser object may be null?

Edit: Comment..

If the exception is that the sequence does not contain elements, the generated query does not return results. I suggest you check the generated query and test it directly against SQL Server.

You can do this in several ways.

>Open SQL Profiler and watch
the query being executed.
>Attach TextWriter to DataContext.Log So that you can see the output of the query
(e.g.
below)..
>use LINQPad as the suggested other answer.

.

p>

StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
this.dcLAUNCHOnline.Log = writer;
//Execute Query..
//sb.ToString(); //will contain the sql produced by the LINQ Query

Leave a Comment

Your email address will not be published.