Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Dynamic where condition
Message
De
30/06/2015 16:38:28
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01621606
Message ID:
01621634
Vues:
69
This message has been marked as the solution to the initial question of the thread.
>Hi everybody,
>
>I am contemplating on a better way to implement the following.
>
>I have a wide table called max4sale which has type and department, category, item, lesson_cat, etc. columns. They are populated depending on the type. The structure of this table is pre-defined and we're not going to change it (just to avoid discussion about the table's design).
>
>In VFP I have the following code to check for possible conflicts when a row is inserted / updated in that table:
>
>
>wait window "Checking for time conflicts..." nowait noclear
>		text TO lcSQL TEXTMERGE noshow
>        select COUNT(ID) as conflicts
>        from dbo.max4sale where type = <<VFP2SQL(type)>>
>        and <<thisform.cWhere>>
>        AND Start_Time >= <<VFP2SQL(Start_Time)>>
>	    and Start_time <= <<VFP2SQL(End_Time)>>
>	    and ID <> <<VFP2SQL(Id)>>
>		ENDTEXT
>
>		*    _cliptext = m.lcSQL
>
>		mysqlexec(m.lcSQL, 'csrTemp', program())
>
>
>where thisform.cWhere was constructed in form's Init depending on the type.
>
>So, I'm thinking of somehow re-producing that either as LINQ query or as a stored procedure where I'd pass many parameters and use dynamic SQL to construct the query.
>
>Assuming I am going to try the first approach of the LINQ query, how can I do that type based condition dynamically, e.g.
>
>if type = 1, then I want to check department column, for example. If type = 2, then department and category, etc.
>
>
>Thanks in advance.
// Set the initial query to find overlapping times. 
var query = dbContext.max4sale.Where(m4s => m4s.Start_Time < this.End_Time && m4s.End_Time > this.Start_Time && m4s.ID != this.ID);

// Add additional filters based on the type
switch(type)
{
    case 1:
        query = query.Where(m4s => m4s.Type == 1);
        break;
    case 2:
        query = query.Where(m4s => m4s.Type == 1 || m4s.Type == 2);
        break;
}

// Get the count of conflicts
var conflicts = query.Count(); // Or .Any() if you just need to know if there is conflicts.
You could also implement this as a validation attribute (https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute%28v=vs.110%29.aspx). See http://blogs.microsoft.co.il/shimmy/2012/01/23/uniqueattribute-that-validates-a-unique-field-against-its-fellow-rows-in-the-database-inherits-dataannotationsvalidationattribute/ for an example of a validation attribute that compares against other records.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform