Solved

I want to do searching on the basis of first and third character i.e. a*d using LINQ from an xml file.How should i do it?

Posted on 2014-10-20
4
114 Views
Last Modified: 2016-02-16
Dear All

I have an XML file in which I am searching city's on the basis of filter containing first three characters,which is working fine.
 Locations = from data in doc.Root.Elements(rs + "data")
                                    // Get access to the child nodes using the XNamespace z
                                    from city in data.Elements(z + "row").Attributes("CITY")
                                    // Filter the result set
                                 where city.Value.StartsWith(substr)
                                    select city;
Now I want to do searching on the basis of only first and third character,second character can be anything.
How should I do it?
0
Comment
Question by:BYTECHINDIA
  • 3
4 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi BYTECHINDIA;

Modifying the query like this should give you what you are looking for.

// Searching for first and third character in the CITY name
// Test data
string first = "A";
string third = "l";
 
Locations = from data in doc.Root.Elements(rs + "data")
            // Get access to the child nodes using the XNamespace z
            from city in data.Elements(z + "row").Attributes("CITY")
            // Filter the result set - Modified line in query
            where city.Value.StartsWith(first) && city.Value.SubString(2, 1) == third 
            select city;

Open in new window

0
 

Author Comment

by:BYTECHINDIA
Comment Utility
ohh it was too simple.Actually I have started looking using SQLMethod function and get lost somewhere else.
Thank you sir,I will try this.
0
 

Author Comment

by:BYTECHINDIA
Comment Utility
Its working perfectly.Can I check two attributes too using same LINQ query i.e. state is Delhi and city starts with filter?If yes,please explain.
0
 

Author Comment

by:BYTECHINDIA
Comment Utility
Thank you sir.
I did it as below and succeeded.

 IEnumerable<XElement> Region = from data in doc.Root.Elements(rs + "data")
                                               from el in data.Elements(z + "row")
                                               where el.Attribute("REGION_NAME").Value == State
                                               select el;

                Locations = from city in Region.Attributes("CITY")
                            where city.Value.StartsWith(Filter)
                            select city;
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now