Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# LINQ SkipLast विधि

SkipLast() विधि का उपयोग करके अंत से तत्वों को छोड़ें और शेष तत्व लौटाएं।

उदाहरण

निम्न एक सरणी है:

int[] marks = { 45, 88, 50, 90, 95, 85 };

अब SkipLast() और लैम्ब्डा अभिव्यक्ति का उपयोग करके अंत से दो तत्वों को छोड़ें, लेकिन यह तत्वों को अवरोही क्रम में व्यवस्थित करने के बाद किया जाता है:

IEnumerable<int> selMarks = marks.AsQueryable()
    .OrderByDescending(s => s)
    .SkipLast(2);

पूर्ण कोड

using System;
using System.Linq;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        int[] marks = { 45, 88, 50, 90, 95, 85 };
        IEnumerable<int> selMarks = marks.AsQueryable()
            .OrderByDescending(s => s)
            .SkipLast(2);
        
        Console.WriteLine("अंतिम दो छात्रों के अंक छोड़े गए...");
        foreach (int res in selMarks)
            Console.WriteLine(res);
    }
}

आउटपुट

अंतिम दो छात्रों के अंक छोड़े गए...
95
90
88
85
  1. - C# LINQ Intersect विधि

    Intersect() विधि का उपयोग करके दो सरणियों के बीच सामान्य तत्व ढूंढें। उदाहरण निम्न हमारी सरणियाँ हैं: int[] val1 = { 15, 20, 40, 60, 75, 90 }; int[] val2 = { 17, 25, 35, 55, 75, 90 }; प्रतिच्छेदन करने के लिए: val1.AsQueryable().Intersect(val2); पूर्ण कोड using System; using System.Collections.Generi

  1. - C# LINQ Where मेथड

    Where मेथड एक सरणी के मानों को एक प्रिडिकेट के आधार पर फ़िल्टर करता है।यहाँ, प्रिडिकेट 70 से ऊपर के तत्वों की जांच कर रहा है।Where((n, index) => n >= 70);उदाहरणusing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {  

  1. - C# LINQ Distinct() मेथड

    अलग-अलग तत्व प्राप्त करने के लिए, Distinct() मेथड का उपयोग करें।निम्नलिखित हमारी सूची है जिसमें डुप्लिकेट तत्व हैं।List<int> points = new List<int> { 5, 10, 5, 20, 30, 30, 40, 50, 60, 70 };अब अलग-अलग तत्व प्राप्त करने के लिए:points.AsQueryable().Distinct();आइए पूरा उदाहरण देखें:उदाहरणusi