English 中文(简体)
利用LINQK搜索整个数据库
原标题:Using LINQ to SQL to search entire database

Is it possible with LINQ to SQL to search the entire database (obviously only the parts that are mapped in the .dbml file) for a string match? I m trying to write a function that will take a string of "Search Term" and search all mapped entities and return a List(Of Object) that can contain a mixture of entities i.e. if I have a table "Foo" and table "Bar" and search for "wibble", if there is a row in "Foo" and one in "Bar" that contain "wibble" i would like to return a List(Of Object) that contains a "Foo" object and a "Bar" object. Is this possible?

问题回答

您的祝福如下:

“当你去图书馆找一本关于植被的书时,你是否走过第一书,开始阅读每本书,看看它是否相关,或者你是否使用某种预先编造的指数,即图书管理员在时间之前为你配置了有用吗?”

如果他说“Well,我将使用该指数”,那么你需要完整的文本索引。

如果他说:“我开始阅读每本书,一本书”,那么你需要新的工作、新的工作,或两种工作:

LINQ to, ORMs in general, 即便是Q也与这种争.不相称。 你正在描述全文检索,因此,你应当使用LQ服务器全文检索功能。 Full Text search自2000年以来所有版本和版本,包括服务器快车。 您需要设立一个财务专家委员会,并撰写问询,在您的问询中利用财务和财政程序数据库。

为什么你们需要这种功能? 除非你特别希望获得可乘之机,否则,这是获得你的数据的......奇怪的......方法。

这可能是可能的,但大多数数据库都通过网络或网络获得,因此其运作非常昂贵。 因此,它像坏的设计一样健全。

还有表格和栏目名称问题,这很可能是你最大的问题。 可以通过反省获得专栏名称,但我不知道表格名称:

foreach (PropertyInfo property in typeof(TEntity).GetProperties())
   yield return property.Name;

edit: @Ben, 您犯错。

这样做可以做到,但不会太好。 有若干可能的解决办法。

<>1>。 填写每张表格的询问,并在你的询问方法中予以执行。

var users = context.Users
    .Where(x => x.FirstName.Contains(txt) || x.LastName.Contains(txt))
    .ToList();

var products = context.Products
    .Where(x => x.ProductName.Contains(txt));

var result = user.Cast<Object>().Concat(products.Cast<Object>());

<>2>。 所有(相关)表格都用于记忆,并使用反思进行搜索。 少用文字来支付巨额业绩影响。

<>3>。 • 利用思考为搜索建造表达树木。 这可能是最佳解决办法,但可能难以实现。

<>4>。 利用设计用于全文检索的材料,例如纳入服务器或阿帕奇卢塞的全文检索。

准则准则的所有解决方案(可能)都要求每张表格进行一次盘问,如果你有许多表格的话,就会对不合格的业绩产生影响。 这里应寻求一种解决办法,将这一询问逐一处理。 我们利用LINQ来利用图书馆处理询问的项目之一,但我不知道它的名称是什么,以及它能做什么,因为我大多数时间都在前班团队工作。

也许,但从我的观点看,这并非建议。 考虑拥有100个表格的记录1000K。 <>低性能> 可通过网站Linq to,在数据库一级制作一个节目,并通过实体打电话。 这比你试图达到的目标快得多。

答案很晚,但既然我只是要为我做一些事情,就在这里。 我写了以下文字,以搜索所有表格的所有栏目,以便进行座标。 这与一项数据法医学任务有关,这项任务交给我,以在大约24GB的数据库中发现所有有线搭桥的痕迹。 在这种规模上,你可以想象,使用曲线器或单一阅读的问询会相当缓慢,搜索整个数据库需要时间。 我写了以下《欧洲航空条约》储存的程序,为我服务机身边做工作,并在《国际公路货运公约》中取得回报,同时迫使平行化。 这令人印象深刻。 整个数据库对标准工程数据库的搜索在不到2秒的时间里完成。 欢乐!

www.un.org/Depts/DGACM/index_spanish.htm 例用:

利用服务器上的所有现有处理器:

EXEC [dbo].[SearchAllTables] @valueSearchTerm =  john michael 

3. 将服务器限制在4个同时校对:

EXEC [dbo].[SearchAllTables] @valueSearchTerm =  john michael , @maxDegreeOfParallelism = 4

以搜索术语使用逻辑操作者:

EXEC [dbo].[SearchAllTables] @valueSearchTerm =  (john or michael) and not jack , @tablesSearchTerm =  not contact 

1. 限制检索表格名称和(或)栏目,包括一些搜索术语:

EXEC [dbo].[SearchAllTables] @valueSearchTerm =  john michael , @tablesSearchTerm =  person contact , @columnsSearchTerm =  address name 

在每一表格中,如发现这些术语,将查询结果限制在第一行:

EXEC [dbo].[SearchAllTables] @valueSearchTerm =  john michael , @getOnlyFirstRowPerTable = 1

只允许每个表格的第一行自动返回原样:

EXEC [dbo].[SearchAllTables] @tablesSearchTerm =  person contact 

仅回答查询:

EXEC [dbo].[SearchAllTables] @valueSearchTerm =  john michael , @tablesSearchTerm =  person contact , @onlyOutputQueries = 1

将成果纳入临时表格和分类:

CREATE TABLE #temp (Result NVARCHAR(MAX));
INSERT INTO #temp
    EXEC [dbo].[SearchAllTables] @valueSearchTerm =  john ;
SELECT * FROM #temp ORDER BY Result ASC;
DROP TABLE #temp;

https://pastebin.com/RRTrt8ZN

我最后撰写了这一少许的 Gem宝(经过搜索后确定的所有配对记录):

namespace SqlServerMetaSearchScan
{
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading;
    using System.Xml;

public class Program
{
    #region Ignition
    public static void Main(string[] args)
    {
        // Defaulting
        SqlConnection connection = null;

        try
        {               
            // Questions
            ColorConsole.Print("SQL Connection String> ");
            string connectionString = Console.ReadLine();
            ColorConsole.Print("Search Term (Case Ignored)> ");
            string searchTerm = Console.ReadLine();
            ColorConsole.Print("Skip Databases (Comma Delimited)> ");
            List<string> skipDatabases = Console.ReadLine().Split( , ).Where(item => item.Trim() != string.Empty).ToList();

            // Search
            connection = new SqlConnection(connectionString);
            connection.Open();

            // Each database
            List<string> databases = new List<string>();
            string databasesLookup = "SELECT name FROM master.dbo.sysdatabases";
            SqlDataReader reader = new SqlCommand(databasesLookup, connection).ExecuteReader();
            while (reader.Read())
            {
                // Capture
                databases.Add(reader.GetValue(0).ToString());
            }

            // Build quintessential folder
            string logsDirectory = @"E:Logs";
            if (!Directory.Exists(logsDirectory))
            {
                // Build
                Directory.CreateDirectory(logsDirectory);
            }
            string baseFolder = @"E:LogsSqlMetaProbeResults";
            if (!Directory.Exists(baseFolder))
            {
                // Build
                Directory.CreateDirectory(baseFolder);
            }

            // Close reader
            reader.Close();                

            // Sort databases
            databases.Sort();

            // New space
            Console.WriteLine(Environment.NewLine + " Found " + databases.Count + " Database(s) to Scan" + Environment.NewLine);

            // Deep scan
            foreach (string databaseName in databases)
            {
                // Skip skip databases
                if (skipDatabases.Contains(databaseName))
                {
                    // Skip 
                    continue;
                }

                // Select the database
                new SqlCommand("USE " + databaseName, connection).ExecuteNonQuery();

                // Table count
                int tablePosition = 1;

                try
                {
                    // Defaulting
                    List<string> tableNames = new List<string>();

                    // Schema examination
                    DataTable table = connection.GetSchema("Tables");

                    // Query tables
                    string tablesLookup = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES";
                    using (SqlDataReader databaseReader = new SqlCommand(tablesLookup, connection).ExecuteReader())
                    {
                        // Get data
                        while (databaseReader.Read())
                        {
                            // Push
                            if (databaseReader.GetValue(0).ToString().Trim() != string.Empty)
                            {
                                tableNames.Add(databaseReader.GetValue(0).ToString());
                            }
                        }

                        // Bail
                        databaseReader.Close();
                    }

                    // Sort
                    tableNames.Sort();                        

                    // Cycle tables
                    foreach (string tableName in tableNames)
                    {
                        // Build data housing
                        string databasePathName = @"E:Logs\SqlMetaProbeResults" + databaseName;
                        string tableDirectoryPath = @"E:LogsSqlMetaProbeResults" + databaseName + @"" + tableName;

                        // Count first
                        int totalEntityCount = 0;
                        int currentEntityPosition = 0;                            
                        string countQuery = "SELECT count(*) FROM " + databaseName + ".dbo." + tableName;
                        using (SqlDataReader entityCountReader = new SqlCommand(countQuery, connection).ExecuteReader())
                        {
                            // Query count
                            while (entityCountReader.Read())
                            {
                                // Capture
                                totalEntityCount = int.Parse(entityCountReader.GetValue(0).ToString());
                            }

                            // Close
                            entityCountReader.Close();
                        }

                        // Write the objects into the houseing
                        string jsonLookupQuery = "SELECT * FROM " + databaseName + ".dbo." + tableName;
                        using (SqlDataReader tableReader = new SqlCommand(jsonLookupQuery, connection).ExecuteReader())
                        {                                                
                            // Defaulting
                            List<string> fieldValueListing = new List<string>();

                            // Read continue
                            while (tableReader.Read())
                            {                                   
                                // Increment
                                currentEntityPosition++;

                                // Defaulting
                                string identity = null;

                                // Gather data
                                for (int i = 0; i < tableReader.FieldCount; i++)
                                {
                                    // Set
                                    if (tableReader.GetName(i).ToUpper() == "ID")
                                    {
                                        identity = tableReader.GetValue(0).ToString();
                                    }
                                    else
                                    {
                                        // Build column data entry
                                        string thisColumn = tableReader.GetValue(i) != null ? " " + tableReader.GetValue(i).ToString().Trim() + " " : string.Empty;

                                        // Piece
                                        fieldValueListing.Add(thisColumn);
                                    }
                                }

                                // Path-centric
                                string explicitIdentity = identity ?? Guid.NewGuid().ToString().Replace("-", string.Empty).ToLower();
                                string filePath = tableDirectoryPath + @"" + "Obj." + explicitIdentity + ".json";
                                string reStringed = JsonConvert.SerializeObject(fieldValueListing, Newtonsoft.Json.Formatting.Indented);
                                string percentageMark = ((double)tablePosition / (double)tableNames.Count * 100).ToString("#00.0") + "%";                                                      
                                string thisMarker = Guid.NewGuid().ToString().Replace("-", string.Empty).ToLower();
                                string entityPercentMark = string.Empty;
                                if (totalEntityCount != 0 && currentEntityPosition != 0)
                                {
                                    // Percent mark
                                    entityPercentMark = ((double)currentEntityPosition / (double)totalEntityCount * 100).ToString("#00.0") + "%";
                                }

                                // Search term verify
                                if (searchTerm.Trim() != string.Empty)
                                {
                                    // Search term scenario
                                    if (reStringed.ToLower().Trim().Contains(searchTerm.ToLower().Trim()))
                                    {
                                        // Lazy build
                                        if (!Directory.Exists(tableDirectoryPath))
                                        {
                                            // Build
                                            Directory.CreateDirectory(tableDirectoryPath);
                                        }

                                        // Has the term
                                        string idMolding = identity == null || identity == string.Empty ? "No Identity" : identity;
                                        File.WriteAllText(filePath, reStringed);
                                        ColorConsole.Print(percentageMark + " => " + databaseName + "." + tableName + "." + idMolding + "." + thisMarker + " (" + entityPercentMark + ")", ConsoleColor.Green, ConsoleColor.Black, true);
                                    }
                                    else
                                    {
                                        // Show progress                                            
                                        string idMolding = identity == null || identity == string.Empty ? "No Identity" : identity;
                                        ColorConsole.Print(percentageMark + " => " + databaseName + "." + tableName + "." + idMolding + "." + thisMarker + " (" + entityPercentMark + ")", ConsoleColor.Yellow, ConsoleColor.Black, true);
                                    }
                                }
                            }

                            // Close
                            tableReader.Close();
                        }

                        // Increment
                        tablePosition++;
                    }                        
                }
                catch (Exception err)
                {
                    ColorConsole.Print("DB.Tables!: " + err.Message, ConsoleColor.Red, ConsoleColor.White, false);                        
                }
            }
        }
        catch (Exception err)
        {
            ColorConsole.Print("KABOOM!: " + err.ToString(), ConsoleColor.Red, ConsoleColor.White, false);                
        }
        finally
        {
            try { connection.Close(); }
            catch { }
        }


        // Await
        ColorConsole.Print("Done.");
        Console.ReadLine();
    }
    #endregion

    #region Cores
    public static string GenerateHash(string inputString)
    {
        // Defaulting
        string calculatedChecksum = null;

        // Calculate
        SHA256Managed checksumBuilder = new SHA256Managed();
        string hashString = string.Empty;
        byte[] hashBytes = checksumBuilder.ComputeHash(Encoding.ASCII.GetBytes(inputString));
        foreach (byte theByte in hashBytes)
        {
            hashString += theByte.ToString("x2");
        }
        calculatedChecksum = hashString;

        // Return
        return calculatedChecksum;
    }
    #endregion


    #region Colors
    public class ColorConsole
    {
        #region Defaulting
        public static ConsoleColor DefaultBackground = ConsoleColor.DarkBlue;
        public static ConsoleColor DefaultForeground = ConsoleColor.Yellow;
        public static string DefaultBackPorch = "                                                                              ";
        #endregion

        #region Printer Cores
        public static void Print(string phrase)
        {
            // Use primary
            Print(phrase, DefaultForeground, DefaultBackground, false);
        }
        public static void Print(string phrase, ConsoleColor customForecolor)
        {
            // Use primary
            Print(phrase, customForecolor, DefaultBackground, false);
        }
        public static void Print(string phrase, ConsoleColor customBackcolor, bool inPlace)
        {
            // Use primary
            Print(phrase, DefaultForeground, customBackcolor, inPlace);
        }
        public static void Print(string phrase, ConsoleColor customForecolor, ConsoleColor customBackcolor)
        {
            // Use primary
            Print(phrase, customForecolor, customBackcolor, false);
        }
        public static void Print(string phrase, ConsoleColor customForecolor, ConsoleColor customBackcolor, bool inPlace)
        {
            // Capture settings
            ConsoleColor captureForeground = Console.ForegroundColor;
            ConsoleColor captureBackground = Console.BackgroundColor;

            // Change colors
            Console.ForegroundColor = customForecolor;
            Console.BackgroundColor = customBackcolor;

            // Write
            if (inPlace)
            {
                // From beginning of this line + padding
                Console.Write("
" + phrase + DefaultBackPorch);
            }
            else
            {
                // Normal write
                Console.Write(phrase);
            }

            // Revert
            Console.ForegroundColor = captureForeground;
            Console.BackgroundColor = captureBackground;
        }
        #endregion
    }
    #endregion
}
}




相关问题
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. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签