tags:

views:

168

answers:

3

In LINQPad is there any way to access either the SYSOBJECTS table or the various INFORMATION_SCHEMA.xxx views using LINQ?

I spend a lot of time searching through our huge company database for partial names as there are too many tables and Stored Procedures to remember the names of them all.

I know I can enter and run SQL in LINQPad but I would like to do this in LINQ instead of SQL as LINQ is more fun :)

Thanks

Xanthalas

A: 

from d in Databases select d

when database connection in LINQPad points to master database.

netmatrix01
That will return all the databases on the server but it won't allow me to search for tables within one of those databases. What I would like is the LINQ equivalent of "select * from SYSOBJECTS where name like '%partialName%' and xtype = 'U'". Thanks.
Xanthalas
A: 

create a new table with the contents of SYSOBJECTS and then search within the new table

select * into SYSOBJECTS_COPY from SYS.OBJECTS

from o in SYSOBJECTS_COPY.AsEnumerable() where Regex.IsMatch( d.Name, "partialName", RegexOptions.IgnoreCase ) select o

gweddington
The only downside I can see to this is that the copy would go stale as changes were made to the database and so would need refreshing periodically. This wouldn't be a big deal for me though so it is a good solution. Thanks gweddington.
Xanthalas
That's a good point, it would probably be better to create a view instead of a table.
gweddington
A: 

You can also embed SQL into your LINQ statements like so:

void Main()
{
    var matches = this.ExecuteQuery<SysObject>("SELECT name, type_desc AS "
                + "TypeDesc FROM [sys].[objects]");

    foreach(var match in matches)
        Console.WriteLine("{0,-30}{1}", (match.Name + ":"), match.TypeDesc);
}

// Define other methods and classes here
class SysObject
{
    public string Name;
    public string TypeDesc;
    // etc...
}

By default LinqPad doesn't use a monospaced font for results, but you can easily change it by pasting the following bit of css into "Edit -> Preferences -> Results -> Launch Editor"

body { font-family: Consolas, monospace; }

Nick Knowlson
I didn't know you could do that with LINQ; that's handy. Thanks Nick.
Xanthalas