Is it possible to somehow compare a value to a range of value without using for/foreach.

IEnumerable<XElement> results = xdoc.Descendants( "PAGE" ).Where( p => p.Attribute( "id" ).Value == RANGE_OF_ID).Select(result => result);

Is there anyway of doing what I wrote below? RANGE_OF_ID could be a collection.

apegram commented: Good question, keep at it. +1

Recommended Answers

All 4 Replies

Use a join.

Use a join.

Could you be a little more explicit

Join sample.

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

class Program
{
    static void Main(string[] args)
    {
        List<Product> products = new List<Product>()
        {
            new Product() { ProductID = 1, ProductName = "Product A" },
            new Product() { ProductID = 2, ProductName = "Product B" },
            new Product() { ProductID = 3, ProductName = "Product C" },
            new Product() { ProductID = 4, ProductName = "Product D" },
            new Product() { ProductID = 5, ProductName = "Product E" },
        };

        List<int> validIDs = new List<int>() { 1, 3, 5 };

        // using lambdas and extension methods
        var productQuery = products.Join(validIDs, product => product.ProductID, validID => validID, (product, validID) => product).Select(product => product);

        foreach (Product product in productQuery)
        {
            Console.WriteLine("{0}\t{1}", product.ProductID, product.ProductName);
        }

        // using query expression
        var productQuery2 = from product in products
                            join validID in validIDs
                            on product.ProductID equals validID
                            select product;

        Console.WriteLine(string.Empty);
        foreach (Product product in productQuery2)
        {
            Console.WriteLine("{0}\t{1}", product.ProductID, product.ProductName);
        }

        Console.Read();
    }

    class Product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
    }
}

Thank you for this, it works like a charm. LINQ is definately a good friend of mine!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.