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
123 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40392202
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
ID: 40393614
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
ID: 40393616
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
ID: 40394398
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

696 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