Tech blog

Solving problems

About
Contact

Archive for the ‘linq’ tag

Random sorting using LINQ

leave a comment

Random Sort

Consider the below Person class:

public class Person
{
    public int Id
    {
        get;
        set;
    }
    public string Name
    {
        get;
        set;
    }
}

This is how you can randomly sort the List object:

List list = new List();

list.Add(new Person { Id = 1, Name = "Davolio Nancy" });
list.Add(new Person { Id = 2, Name = "Fuller Andrew" });
list.Add(new Person { Id = 3, Name = "Leverling Janet" });
list.Add(new Person { Id = 4, Name = "Peacock Margaret" });
list.Add(new Person { Id = 5, Name = "Buchanan Steven" });
list.Add(new Person { Id = 6, Name = "Suyama Michael" });
list.Add(new Person { Id = 7, Name = "King Robert" });
list.Add(new Person { Id = 8, Name = "Callahan Laura" });
list.Add(new Person { Id = 9, Name = "Dodsworth Anne" });

list = list.OrderBy(x => Guid.NewGuid()).ToList();

Or you can use this approach:

Random random = new Random();
list = (from x in list
       let r = random.Next()
       orderby r
       select x).ToList();
VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Written by Avivo

July 15th, 2010 at 10:07 am

Create Linq to SQL classes from .sdf (Local database)

leave a comment

Linq allows to handle data easily. Linq to SQL tools are integrated in Visual Studio but is limited to can generate code only for  certain databases. To create ORM from .sdf database file read on…

What to do?
Copy next script to a file with .bat extension.

Change DATABASE_NAME, DATABASE_PASSWORD and NAMESPACE according to your needs.

@echo off
rem Input parameters
rem DATABASE_NAME should NOT contain spaces
set DATABASE_NAME=MyDatabase
set DATABASE_PASSWORD=mypass
set NAMESPACE=MyNamespace.Database

rem Set full path to SqlMetal.exe
rem For 64-bit Windows
set SQLMETAL="%ProgramW6432%\Microsoft SDKs\Windows\v6.0A\Bin\SqlMetal.exe"
rem For 32-bit Windows
if "%ProgramW6432%"=="" set SQLMETAL="%ProgramFiles%\Microsoft SDKs\Windows\v6.0A\Bin\SqlMetal.exe"

rem Other parameters
set DATABASE_PATH="%CD%\%DATABASE_NAME%.sdf"
set OUTPUT_CS_PATH="%CD%\%DATABASE_NAME%.cs"
set OUTPUT_DBML_PATH="%CD%\%DATABASE_NAME%.dbml"
set CONNECTION_STRING="Data Source=\"%DATABASE_PATH%\"; Password=\"%DATABASE_PASSWORD%\";"

rem Check and run sqlmetal
if not exist %SQLMETAL% goto error-missing-sqlmetal
%SQLMETAL% /conn:%CONNECTION_STRING% /dbml:%OUTPUT_DBML_PATH% /namespace:%NAMESPACE% /context:%DATABASE_NAME%DataContext /pluralize
%SQLMETAL% /conn:%CONNECTION_STRING% /code:%OUTPUT_CS_PATH% /namespace:%NAMESPACE% /context:%DATABASE_NAME%DataContext /pluralize
goto end

:error-missing-sqlmetal
echo Error: cannot find sqlmetal.exe
echo Search location: %SQLMETAL%
goto end

:end

Example

If the database is stored in C:\db\Website.sdf then copy script into C:\db\make.bat. Change DATABASE_NAME to Website and run the script. Script should generate C:\db\Website.cs and C:\db\Website.dbml files. Include the generated .cs file into your project.

Using the generated code

class Program
{
  static void Main()
  {
    //Suppose that Website.sdf has Page table with CreationDate column of type DateTime
    WebsiteDataContext db = new WebsiteDataContext(@"C:\db\Website.sdf");
    var list = from p in db.Pages
                 where p.CreationDate > DateTime.Today
                 selet p;

    //Show number of pages that match Linq query
    Console.WriteLine(list.Count() + " pages created today.");
  }
}
VN:F [1.9.3_1094]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Written by developer

March 30th, 2010 at 7:53 pm

Posted in Projects

Tagged with , , , , , , ,

The custom tool ‘MSLinqToSQLGenerator’ failed. Could not retrieve the current project.

leave a comment

To correct this, open up the Visual Studio 2008 command prompt (or any command prompt and navigate to the location of the VS exe ‘devenv.exe’) and type the following command:

devenv /ResetSkipPkgs

Then open your LinqToSql file(s) (*.dbml) and save to trigger the code generator tool.  After that, you should hopefully be up and running once more.

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Written by Avivo

September 4th, 2009 at 3:30 pm