September 6, 2010  
  You are here:  Blog
LINQ to Joe Archive
Module Border Module Border
My Book

C#


VB

Module Border Module Border

Using LINQ to Generate Test Data with the Range Operator
 
Location: BlogsLINQ to Joe    
Posted by: Joe Rattz 10/21/2009 12:27:41 PM

Using LINQ to Generate Test Data with the Range Operator

When working on Pro LINQ, one of the operators that caught my attention is the Range operator.  The Range operator generates a sequence of integers.  At the time, this seemed like it could be very useful for generating test data.  I just used this again for a sample application I was working on and thought I should make a blog post about it.

int numItems = 200;

var list = Enumerable.Range(1, numItems)
                     .Select(i => new
                       {
                         Active = i & 1,
                         Id = i,
                         Description = "Site " + i.ToString(),
                         AccountNumber = "01015" + i.ToString("000"),
                         City = "City Ville",
                         State = "GA",
                         Local = i < 25,
                         COJ = i % 3
                       });

SearchCombo.DataSource = list;
SearchCombo.DataBind();

I have created a separate variable named numItems for the number of items I want to generate, but this obviously isn't necessary.  Next I call the Range operator to generate a sequence of integers containing however many items I want in the test.  I then call the Select operator on the sequence of integers and instantiate an anonymous object containing some sample data.  Please notice that I used the integer from the sequence in each anonymous object.  This of course isn't necessary either but makes for unique data.  Some of the anonymous object's members (Local and COJ) are just initialized with a somewhat random expression based on the integer just to give the test data a more random look.

I then bind the generated sequence of test data to a combo box.  In this case, it is an Infragistics' WebCombo control.  Here is a screenshot.

RangeDemo.jpg

Using the Range operator to generate test data can be very handy.

Copyright ©2009 Joe Rattz
Permalink |  Trackback

Your name:
Title:
Comment:
Add Comment   Cancel 
 
 
Home|Forums|Blog|LINQ Extras|Contact Me|Book Reviews
  Copyright (c) 2010 LINQDev Terms Of Use Privacy Statement