Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to declare the variable correctly?
Message
 
 
À
28/09/2012 17:13:48
Information générale
Forum:
ASP.NET
Catégorie:
LINQ
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01553872
Message ID:
01571162
Vues:
47
>You could also do this to allow both filters to work:
>
public ActionResult Search(int? clientNo = null, string clientName = null)
>        {
>            // Assume we want to select everything
>            var clients = Db.Clients; // Should set type of clients to IQueryable<Clients>
>            
>            if ((clientNo??0) != 0) //Number was supplied
>              clients = clients.Where(c => (c.Number == clientNo));
> 
>           // If clientNo was supplied, clients is now filtered by that. If not, it still has the full list. The following will further filter it.
>            if (!String.IsNullOrWhitespace(clientName) ) // Part of the name was supplied
>                clients = clients.Where(c => (c.Name.Contains(clientName)));
>
>           // Order the filtered list
>              clients = clients.OrderBy(c => c.Name);
>
>            return PartialView("_ClientsSearch", clients);
>        }
I have slightly different question now.

I am using this code:
  var operators = Db.Operators;
         String cOperator = formValues["searchOperator"] ?? "";
         String cLastName = formValues["searchLastName"] ?? "";
         String cFirstName = formValues["searchFirstName"] ?? "";

         if (!String.IsNullOrWhiteSpace(cOperator))
            operators = operators.Where(o => o.OperatorCode.StartsWith(cOperator));

         if (!String.IsNullOrWhiteSpace(cLastName))
            operators = operators.Where(o => o.LastName.StartsWith(cLastName));

         if (!String.IsNullOrWhiteSpace(cFirstName))
            operators = operators.Where(o => o.FirstName.StartsWith(cFirstName));
However, if all values are empty, I want my variable to not have rows. I am not sure how to simulate "1=0" with LINQ.

And also, all MVC samples use integer Id for Id. In this particular table I do not have Id integer column, I only have CHAR(6) column which is Primary Key.

How should I call the Edit form then?

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform