Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to declare the variable correctly?
Message
From
28/09/2012 22:46:14
John Baird
Coatesville, Pennsylvania, United States
 
General information
Forum:
ASP.NET
Category:
LINQ
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01553872
Message ID:
01553920
Views:
63
This message has been marked as a message which has helped to the initial question of the thread.
>Hi everybody,
>
>I am trying to write a Search method that can accept two parameters (no or Name). Here is what I've started, but I don't know how should I declare "clients" variable at the top correctly. What is it when used in LINQ query?
>
>
>public ActionResult Search(int? clientNo = null, string clientName = null)
>        {
>            
>            if (((clientNo??0) == 0) && (String.IsNullOrEmpty(clientName)==true))
>            {
>                // Assume we want to select everything
>                var clients = Db.Clients.OrderBy(c => c.Name);
>            }
>            var clients = Db.Clients.Where(c => (c.Number == clientNo)).OrderBy(c => c.Name);
>
>            return PartialView("_ClientsSearch", clients);
>        }
>
>Since I can not declare var clients twice, what will be correct solution here?
>
>Thanks in advance.


Having read the entire thread, I can't believe it. If you couldn't figure this out on your own, maybe you should look for another line of work. This is elementary coding.

Also, var is used to infer the type of a given variable. It cannot stand alone as a definition in that it can't imply the type. You have to assign something to it that can be typed, i.e. var clients = db.clients.... var clients = myClients.GetType() or whatever. In addition, the statement ''String.IsNullOrEmpty(ClientName)" doesn't need the '== true' after it. Boolean values stand on their own as either true of false....
Previous
Reply
Map
View

Click here to load this message in the networking platform