Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to declare the variable correctly?
Message
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:
01553881
Views:
45
>
>public ActionResult Search(int? clientNo = null, string clientName = null)
>        {
>            var clients;
>            if (((clientNo??0) == 0) && (String.IsNullOrEmpty(clientName)==true))
>            {
>                // Assume we want to select everything
>                clients = Db.Clients.OrderBy(c => c.Name);
>            }
>            clients = Db.Clients.Where(c => (c.Number == clientNo)).OrderBy(c => c.Name);
>
>            return PartialView("_ClientsSearch", clients);
>        }
>
>
>>No, this doesn't seem to work, I already tried it.
>
>
>Aren't you wasting time with the if? I mean, if you are going to set clients to something anyways after the if, you might as well remove the if all together or put an else, which is what I suspect you want :)

I want - if I don't pass anything, select everything. If I passed number, select by number. If I passed string, select by string. If I passed both - I am not sure yet, usually we only use one condition. Should I return an error in this case and let the default error handler (default error page) to show up or how exactly this is usually handled?

BTW, as I already said to Boris, it doesn't work this way, I get
Error 1 Implicitly-typed local variables must be initialized

---------------------
BTW, I think I figured this out. I can use var clients to select everything - no conditions and no checks. Then I check if the number is not null and just use
clients = ...

and then do the same for string.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform