Return lambda value and not lambda mounted query

Asked

Viewed 81 times

1

I did that lambda

OfferUri = OfferUri.Select(s => s.Items.Where(a => a.SubscriptionId == s.Id).Select(n => n.Id).FirstOrDefault()).ToString()

and when I see what she returns to me, I have it on the property

SELECT     
        CASE WHEN ([Project5].[C1] IS NULL) 
        THEN 0 ELSE [Project5].[C2] 
        END AS [C1]    
    FROM ( SELECT         
            [Project3].[C1] AS [C1],         
            (SELECT TOP (1)             
                [Extent3].[Id] AS [Id]            
            FROM [dbo].[SubscriptionItem] AS [Extent3]            
            WHERE ([Project3].[Id] = [Extent3].[SubscriptionId]) 
            AND ([Extent3].[SubscriptionId] = [Project3].[Id])) AS [C2]        
            FROM ( SELECT             
                   [Project2].[Id] AS [Id],             
                   [Project2].[C1] AS [C1]            
                   FROM ( SELECT                 
                             [Extent1].[Id] AS [Id],                 
                             (SELECT TOP (1)                     
                                  [Extent2].[Id] AS [Id]                    
                                  FROM [dbo].[SubscriptionItem] AS [Extent2]                    
                               WHERE ([Extent1].[Id] = [Extent2].[SubscriptionId]) 
                               AND ([Extent2].[SubscriptionId] = [Extent1].[Id])) AS [C1]                
                               FROM [dbo].[Subscription] AS [Extent1]                
                               WHERE [Extent1].[OrderId] = 4661 )  AS [Project2] )  AS [Project3])  AS [Project5]

running in the bank get this

C1
---
3845
3848

What I would like to know is how to return these last values in the property and not the query

EDIT1

The whole point is that when I run this query

var csp = OfferUri.Select(s => s.Items.AsEnumerable().Where(a => a.SubscriptionId == s.Id).Select(n => n.CspSubscriptions.Where(sel => sel.OriginSubscriptionItemId == n.Id).Select(r => r.OfferUri))).FirstOrDefault().ToString();

I pick this up

System.Collections.Generic.List`1[System.Collections.Generic.IEnumerable`1[System.String]]

and should take this

031C9E47-4802-4248-838E-778FB1D2CC05
A6C5A400-70B4-458F-AAF9-DADE77A70418

That’s the problem I’m going through, I can’t bring the value of query execution.

1 answer

2

The problem is that when you called the .ToString() the query had not yet been executed, if you put a .ToList() (or .FirstOrDefault() if you do not expect a return list) before it you will solve:

OfferUri = 
    OfferUri
        .Select(s => s.Items
            .Where(a => a.SubscriptionId == s.Id)
                .Select(n => n.Id).FirstOrDefault()).ToList().ToString()

Actually it doesn’t make much sense to use the ToString in a list (I imagine it is a list you expect, because in the result you put of example returns more than one value), you are probably using the ToString by mistake.

  • 1

    Only one fix would be better Use Firstordefault() instead of First() because if the query resume no value it will generate an Exception

  • 1

    @Marcosbrinner, I gave the example of First because I don’t know if the return will always work, but it’s a good observation. I changed the answer to avoid the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.