Wednesday, 16 October 2013

C# Tips : LINQ SelectMany Method C#.Net and VB.Net example

You can query objects using SelectMany LINQ method and get result according to your given criteria. You can also query more than one criteria.

Here is example for this.

In this example we take "books.xml". Now we query on element "author" whose sub element is 'firstname' and 'lastname' and we only want list of authors whose name is either 'Pratik' or 'David'.



C#. Net Example :
    public IEnumerable<XElement> GetBooks(string author)
    {
        XDocument objDoc = XDocument.Load(Server.MapPath("Books.xml"));
        var objQuery = objDoc.Descendants("Book")
                        .Where(b => b.Elements("Author")
                                     .Elements("FirstName")
                                     .Any(auth => (string)auth == author));

        return objQuery;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        string[] strAuthors = { "Pratik", "David" };
        var books = strAuthors.SelectMany(n => GetBooks(n));
        foreach (var objBook in books)
        {
            Response.Write("<b>Book Title : </b>" + objBook.Element("Title").Value + "   <b>Author Name : </b>" + objBook.Element("Author").Element("FirstName").Value);
            Response.Write("<br/>");
        }
    }

VB.Net Examples :
    Public Function GetBooks(author As String) As IEnumerable(Of XElement)
        Dim objDoc As XDocument = XDocument.Load(Server.MapPath("Books.xml"))
        Dim objQuery = objDoc.Descendants("Book").Where(Function(b) b.Elements("Author").Elements("FirstName").Any(Function(auth) auth = author))
        Return objQuery
    End Function


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strAuthors As String() = {"Pratik", "David"}
        Dim books = strAuthors.SelectMany(Function(n) GetBooks(n))
        For Each objBook In books
            Response.Write(("<b>Book Title : </b>" + objBook.Element("Title").Value & "   <b>Author Name : </b>") + objBook.Element("Author").Element("FirstName").Value)
            Response.Write("<br/>")
        Next
    End Sub
Books.xml File : 
<?xml version="1.0" encoding="utf-8" ?>
<Books>
    <Book >
        <Title>ASP.NET</Title>
        <ISBN>asp1</ISBN>
        <ReleaseDate>11/11/2010</ReleaseDate>
        <Pages>200</Pages>
        <PublisherId>1</PublisherId>
        <Author>
            <FirstName>Pratik</FirstName>
            <LastName>Patel</LastName>
        </Author>
    </Book>
    <Book>
        <Title>C#.NET</Title>
        <ISBN>c#2</ISBN>
        <ReleaseDate>10/11/2010</ReleaseDate>
        <Pages>500</Pages>
        <PublisherId>1</PublisherId>
        <Author>
            <FirstName>David</FirstName>
            <LastName>Anderson</LastName>
        </Author>
    </Book>
    <Book>
        <Title>VB.NET</Title>
        <ISBN>vb3</ISBN>
        <ReleaseDate>5/5/2009</ReleaseDate>
        <Pages>400</Pages>
        <PublisherId>1</PublisherId>
        <Author>
            <FirstName>Roger</FirstName>
            <LastName>Z.</LastName>
        </Author>
    </Book>
    <Book>
        <Title>SQL Server</Title>
        <ISBN>sql4</ISBN>
        <ReleaseDate>6/9/2010</ReleaseDate>
        <Pages>300</Pages>
        <PublisherId>2</PublisherId>
        <Author>
            <FirstName>Pratik</FirstName>
            <LastName>Patel</LastName>
        </Author>
    </Book>
    <Book>
        <Title>JAVA</Title>
        <ISBN>java5</ISBN>
        <ReleaseDate>8/5/2011</ReleaseDate>
        <Pages>400</Pages>
        <PublisherId>3</PublisherId>
        <Author>
            <FirstName>David</FirstName>
            <LastName>Anderson</LastName>
        </Author>
    </Book>
    <Book>
        <Title>HTML</Title>
        <ISBN>html6</ISBN>
        <ReleaseDate>9/5/2011</ReleaseDate>
        <Pages>400</Pages>
        <Author>
            <FirstName>Roger</FirstName>
            <LastName>Z.</LastName>
        </Author>
    </Book>
</Books>

Output : 


  • Visit this link for MSDN Help for SelectMany Method. Click Here...


Below are the books that you would like :

No comments:

Post a Comment