Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In LINQPad can you access SYSOBJECTS using LINQ?

Tags:

.net

linq

linqpad

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

like image 628
Xanthalas Avatar asked May 14 '10 10:05

Xanthalas


1 Answers

Yes you can.

All you have to do is include system views and SPs in selected connection and use LINQ like following:

sys.Sysobjects.Where(sp => sp.Xtype == "P")  // returns SPs
sys.Sysobjects.Where(t => t.Xtype == "U")    // returns Tables

or using sys.Views directly [example returns all tables with columns containing string "person" ]:

sys.Tables.Join(sys.Columns,
                t => t.Object_id,
                c => c.Object_id,
                (t, c) => new { t, c })
    .Where(x => x.c.Name.Contains("person"))
    .Select(x => new { ObjName = x.t.Name,
                       ChildName = x.c.Name } )
            .Distinct()
like image 68
karol Avatar answered Oct 15 '22 23:10

karol