How to get limited number of outputs from the query c#

Anitha Sundaramoorthy

I need to get only the first value of the query. How can I do that ?

Dictionary<int, string> dict = new Dictionary<int, string>();

dict.Add(1, "Jack");
dict.Add(2, "Peter");
dict.Add(3, "Chris");
dict.Add(4, "Peter");

var keys = from entry in dict where entry.Value == "Peter" select entry.Key limit 1;

I get an error if I use limit. So what is other ways to limit the output or how to get the first result alone from the query ?

Error CS0103 The name 'limit' does not exist in the current context

AD8

You could also try this syntax...

var key = dict.FirstOrDefault(v => v.Value == "Peter").Key;

Edit: Added code for ease of understanding / copy-pasting... Rextester - http://rextester.com/AIAKRZ95654

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {

            Dictionary<int, string> dict = new Dictionary<int, string>();

            dict.Add(1, "Jack");
            dict.Add(2, "Peter");
            dict.Add(3, "Chris");
            dict.Add(4, "Peter");

            var key = dict.FirstOrDefault(v => v.Value == "Peter").Key;

            Console.WriteLine(key);
        }
    }
}

UPDATE: Please note when using FirstOrDefault() keyword, ?.key is not required, worst case it will return 0. The ?.key is required when using First(). Because of this confusion Flater voted this answer down, and deleted his comments from the comment section below. (He probably should have acknowledge and mentioned this factor to give other programmers a heads up / caution sign about this subtle difference between First() and FirstOrDefault())

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How get limited number of input in Haskell

How to get the only value of a limited SQL query

how to export limited number of rows from heroku

How to limit number of outputs from external URL

How to get the limited result from Django query when filtering with a list of IDs.?

How To Get Multiple Outputs to Show From a For Loop

How to get readable command outputs from terminal

function to get nth letter from a string outputs number 1

How to get limited number of rows based on a DISTINCT values

How to count number of outputs?

How to format outputs from printf in c

How to get the number of inserts from a raw INSERT query in Django?

How to get Total number of Rows from a query with also all column

How to get Remaining outputs from stored procedure with multiple returns in C#

Query a limited items from the database

How to create a limited number of threads?

To get limited images from firebase

how to extract a number from text file (or do a query on a site to get that number to bat file

How can I get the sum of the outputs of an equation from a for loop?

How to get hidden layer/state outputs from a Bert model?

How to exclude taint nodes from outputs in kubectl get nodes?

How could I get a variety of outputs from a shell script?

How can you get numeric outputs from an alphanumeric(Pyautogui)

How to get the outputs from a CONNECT platform internal programatic call?

How to get different outputs from the same Keras layer and then combine them?

get limited number of rows per joined column

Get a limited number of rows for each match in Postgres

Get the number of the line, from response, that matches the query

Get number of documents returned from a query

TOP Ranking

HotTag

Archive