English 中文(简体)
在LINQPad,你能否利用LINQ进入SYSOBJECTS?
原标题:In LINQPad can you access SYSOBJECTS using LINQ?

在LINQPad,是否有任何办法利用LINQ获取SYSOBJECTS表或各种信息。

我花了很多时间通过我们庞大的公司数据库搜索部分名称,因为有太多的表格和仓储程序来记住他们的名字。

我知道,我可以进入和操作LQPad,但我想在LINQ中这样做,而LINQ则更是说:

增 编

Xanthalas

最佳回答

制作新表格,列出SYSOBJECTS的内容,然后在新桌旁搜寻

select * into SYSOBJECTS_COPY from SYS.OBJECTS

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

是的。

你们必须做的是将系统观点和特殊产品纳入选定的联系,并使用准则,如:

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

或者使用玩具。 直接[包括所有含有“人”栏的表格]:

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()

你们也可以把“文化与文化”的发言纳入到“文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化

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...
}

由于缺席,LinqPad不使用单一空间的字体来取得结果,但你可以轻易改变,把以下的圆块 c带入“Edit ->P惠s-> 结果-> 发射编辑”。

body { font-family: Consolas, monospace; }

该法典还回归了目标定义,如果你想要的话,允许你在定义中查询。

void Main()
    {
        var matches = FetchObjects(true);

        var searchTerm = "tblName"; //<--Change this to filter for what you are looking for
        bool searchName = true; //search the object name
        bool searchDef = false; //search inside object definition (ie the stored procedure definition)
        TypeDescs typeDesc = TypeDescs.Any; //specify whether you want to limit your search to tables or stored procedures

        matches
            .Where(x=> (
                (searchName && x.Name.Contains(searchTerm)) 
                || (searchDef && (x.ObjectDefinition!=null && x.ObjectDefinition.Contains(searchTerm))) )
                && (typeDesc==TypeDescs.Any || x.TypeDesc == typeDesc.ToString())

                )
            .Select(x=> new {x}).Dump();

    }
    IEnumerable<SysObject> FetchObjects(bool includeDefinitions){
         return this.ExecuteQuery<SysObject>("SELECT Name=convert(varchar(30), name), type_desc AS " 
                    + " TypeDesc "
                    + string.Format(", ObjectDefinition={0}", (includeDefinitions)?"OBJECT_DEFINITION (OBJECT_ID(name))":"NULL")
                    + " FROM [sys].[objects]");
    }
    enum TypeDescs {Any, SQL_STORED_PROCEDURE, USER_TABLE}
    class SysObject 
    { 
        public string Name; 
        public string TypeDesc; 
        public string ObjectDefinition;
    } 
from d in Databases
select d

当LINQPad数据库连接到主数据库时。

除了@Nicks的回答外,here是产生标识表信息的幻灯片,并在VS法典中开放。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签