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

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2128 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.