Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
ToList.Where not producing expected results
Message
 
 
To
30/10/2012 07:22:11
General information
Forum:
ASP.NET
Category:
LINQ
Miscellaneous
Thread ID:
01556016
Message ID:
01556047
Views:
18
ToList actually executes the SQL Statement and produces the result. Since it preceds where I suspect it first gets everything (executes SQL without where) and then will be applying where condition to the produced result.

You can run the SQL profiler while running LINQ queries and you'll see when the SQL is actually executed and when not.


>Thanks, I had already told him about moving the complex expression to before the foreach and just referring to the variable afterwards.
>
>I will pass on the message of reducing the code in the view.
>
>What exactly does the ToList() do? From how the code was written I had thought it pulled the list of data from the model (but I've never done this MVC thing). From your and Viv's response it looks like you expect all the data to be available automatically. Is that right?
>
>>1. I don't think we need ToList here at all. Also, I would put this complex expression into a variable (especially since it's used a few times).
>>
>>Finally, why does he put it in the view code instead of the controller's code? Isn't it too complex for the view (putting too much code in the view is not a good practice).
>>
>>>Hi,
>>>
>>>another developer who I'v e helped in the past has asked me to help him work out why he is not getting the expected data with the following code:
>>>
>>>
@foreach (var claims in Model.Claims.ToList().Where(w => w.memberid == User.Identity.Name.Substring(User.Identity.Name.IndexOf('(') + 1, (User.Identity.Name.IndexOf(')') - 1) - User.Identity.Name.IndexOf('('))) )
>>>                    {
>>>                        var uid = User.Identity.Name.Substring(User.Identity.Name.IndexOf('(') + 1, (User.Identity.Name.IndexOf(')') - 1) - User.Identity.Name.IndexOf('('));
>>>                        var mid = claims.memberid;
>>>                        if (uid == mid)
>>>                        {
>>>                            <tr class="odd-row">
>>>                                <td>N/A</td>
>>>                                <td>@claims.healthplanid</td>
>>>                                <td>@claims.claimnumber</td>
>>>
>>>
>>>Unfortunately I do not have a clue (having never used LINQ, which is what I think this code is using) but I've asked him how come the first line (with the ToList().Where) doesn't just pull out the correct set of records and he doesn't know.
>>>
>>>The User.Identity.Name should contain a string like this: Frank Cazabon (214-00-0121)
>>>
>>>But apparently it is pulling out lots of other data but not the expected set.
>>>
>>>Can any one explain why?
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