locked
How do you get values out of a Linq Query result without using a foreach loop? RRS feed

  • Question

  • User2130491911 posted

    Not sure that I have this correct but I am returning data to an mvc view using ajax/JSON and I have a function that goes out an queries the database... grabs some data that I format using string builder and then I return it to the mvc razor view....

    What I am getting back is an actual query and not the data:

    Please... have a look:

                var ConResults = from c in db.CM_CONTESTANTS_MASTER_DETAIL_TBL
                                     where c.MC_CONTESTANT_ID == Contestant_ID
                                     select new
                                     {
                                         FirstName = c.MC_CONTESTANT_FIRST_NAME,
                                         LastName = c.MC_CONTESTANT_LAST_NAME,
                                         DOB = c.MC_CONTESTANT_DOB,
                                         VLink = c.MC_CONTESTANT_VIDEO_FILE
    
    
                                     };
    
                var fn = ConResults.Select(i => i.FirstName.ToString());
                var ln = ConResults.Select(i => i.LastName.ToString());
                var dob = ConResults.Select(i => i.DOB.ToString());
                var vl = ConResults.Select(i => i.VLink.ToString());
    
                var sb = new StringBuilder();
    
                sb.Append(fn + " " + ln);
                sb.AppendLine();
                sb.Append(dob);
                sb.AppendLine();
                sb.Append(vl);
    
                ContestantData = sb.ToString();

    The outcome I am looking for is to have the values that get collected in the ConResults

    FirstName LastName DoB and so one.

    These are the results I am getting which tells me I may be doing something wrong... :)

    SELECT 
        CASE WHEN ([Extent1].[MC_CONTESTANT_FIRST_NAME] IS NULL) THEN N'' ELSE [Extent1].[MC_CONTESTANT_FIRST_NAME] END AS [C1]
        FROM [db_BOT].[CM_CONTESTANTS_MASTER_DETAIL_TBL] AS [Extent1]
        WHERE [Extent1].[MC_CONTESTANT_ID] = @p__linq__0 SELECT 
        CASE WHEN ([Extent1].[MC_CONTESTANT_LAST_NAME] IS NULL) THEN N'' ELSE [Extent1].[MC_CONTESTANT_LAST_NAME] END AS [C1]
        FROM [db_BOT].[CM_CONTESTANTS_MASTER_DETAIL_TBL] AS [Extent1]
        WHERE [Extent1].[MC_CONTESTANT_ID] = @p__linq__0
    SELECT 
        CASE WHEN ([Extent1].[MC_CONTESTANT_DOB] IS NULL) THEN N'' ELSE  CAST( [Extent1].[MC_CONTESTANT_DOB] AS nvarchar(max)) END AS [C1]
        FROM [db_BOT].[CM_CONTESTANTS_MASTER_DETAIL_TBL] AS [Extent1]
        WHERE [Extent1].[MC_CONTESTANT_ID] = @p__linq__0
    SELECT 
        CASE WHEN ([Extent1].[MC_CONTESTANT_VIDEO_FILE] IS NULL) THEN N'' ELSE [Extent1].[MC_CONTESTANT_VIDEO_FILE] END AS [C1]
        FROM [db_BOT].[CM_CONTESTANTS_MASTER_DETAIL_TBL] AS [Extent1]
        WHERE [Extent1].[MC_CONTESTANT_ID] = @p__linq__0

    Any Ideas would be appreciated 

    Monday, September 14, 2020 7:49 PM

All replies

  • User2130491911 posted

    Err Found the answer its first or default 

              var Results = db.CM_CONTESTANTS_MASTER_DETAIL_TBL.FirstOrDefault(c => c.MC_CONTESTANT_ID == Cont

    Monday, September 14, 2020 7:59 PM