Best way of constructing dynamic sql queries in C#/.NET3.5?

后端 未结 12 2890
走了就别回头了
走了就别回头了 2021-02-15 11:25

A project I\'m working on at the moment involves refactoring a C# Com Object which serves as a database access layer to some Sql 2005 databases.

The author of the existe

12条回答
  •  春和景丽
    2021-02-15 11:30

    I used C# and Linq to do something similar to get log entries filtered on user input (see Conditional Linq Queries):

    IQueryable matches = m_Locator.Logs;
    
    // Users filter
    if (usersFilter)
        matches = matches.Where(l => l.UserName == comboBoxUsers.Text);
    
     // Severity filter
     if (severityFilter)
         matches = matches.Where(l => l.Severity == comboBoxSeverity.Text);
    
     Logs = (from log in matches
             orderby log.EventTime descending
             select log).ToList();
    

    Edit: The query isn't performed until .ToList() in the last statement.

提交回复
热议问题