LINQ to parse EventLog Query [modified]
-
Hi All, I want LINQ query to retrieve records from eventlog viewer with like operator. Is there any LIKE operator in LINQ? please help me. Regards, Sunil G.
modified on Wednesday, February 3, 2010 7:25 AM
You could just use
String.StartsWith()
[^]. /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
Hi All, I want LINQ query to retrieve records from eventlog viewer with like operator. Is there any LIKE operator in LINQ? please help me. Regards, Sunil G.
modified on Wednesday, February 3, 2010 7:25 AM
Bear in mind that the event log entries are just a collection, you can retrieve items from it using something like the following:
var item = from EventLogEntry p in EventLog.GetEventLogs().First().Entries
where p.Message.Contains("Online")
select new { Id = p.InstanceId, Message = p.Message };In this sample, I'm just picking from the first event log - you would probably want to refine this to find the log you are interested in explicitly. The Contains method is the closest you'll get to the Like operator.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Hi All, I want LINQ query to retrieve records from eventlog viewer with like operator. Is there any LIKE operator in LINQ? please help me. Regards, Sunil G.
modified on Wednesday, February 3, 2010 7:25 AM
You can use the below code if you using LINQ to Objects
private void button1_Click(object sender, EventArgs e)
{
List<Customer> customers = new List<Customer>();
customers.Add(new Customer { CustomerID = "A12I", CustomerName = "Palash" });
customers.Add(new Customer { CustomerID = "B20G", CustomerName = "Ankesh" });
customers.Add(new Customer { CustomerID = "A100I", CustomerName = "Subhadip" });
customers.Add(new Customer { CustomerID = "C12G", CustomerName = "Namita" });
var result = (from c in customers
where c.CustomerID.StartsWith("A") && c.CustomerID.EndsWith("I")
select c).ToList();
foreach (Customer c in result)
{
MessageBox.Show(c.CustomerName);
}
}} public class Customer { public string CustomerID; public string CustomerName; }
for Search A%I . In case Of Search %I% you can use String Contains method to do that In Case of LINQ to SQL You can Use the Syntax like
var result = (from c in db.Customers
where System.Data.Linq.SqlClient.SqlMethods.Like(c.CustomerID, "A%I")
select c).ToList();