LINQ To XML in BlogEngine

Topics: ASP.NET 2.0, Business Logic Layer
Dec 3, 2010 at 12:01 AM
Edited Oct 24, 2011 at 6:23 PM

Hello All,

Trying to figure out how to do LINQ queries in BlogEngine

 

http://msdn.microsoft.com/en-us/library/bb397927.aspx

 

Obtaining a Data Source

In a LINQ query, the first step is to specify the data source. In C# as in most programming languages a variable must be declared before it can be used. In a LINQ query, the from clause comes first in order to introduce the data source (customers) and the range variable (cust).

//queryAllCustomers is an IEnumerable<Customer>
var queryAllCustomers = from cust in customers
select cust;

How do you do this in BlogEngine?

using Data.BlogDataContext context = getNewContext())   Does not work.

 

Ordering

Often it is convenient to sort the returned data. The orderby clause will cause the elements in the returned sequence to be sorted according to the default comparer for the type being sorted. For example, the following query can be extended to sort the results based on the Name property. Because Name is a string, the default comparer performs an alphabetical sort from A to Z.

var queryLondonCustomers3 = 
from cust in customers
where cust.City == "London"
orderby cust.Name ascending
select cust;

How can you do this in BlogEngine?

 

Another Website: http://scruffylookingcatherder.com/post/2009/03/04/Gratuitous-Use-of-Linq.aspx

 

public override Page SelectPage(Guid id)

{

    Page page = null;

///Does not work ///
using (Data.BlogDataContext context = getNewContext()) { Data.Page pageData = (from p in context.Pages where p.PageID == id select p).FirstOrDefault(); if (pageData != null) { page = new Page() { Id = pageData.PageID, Title = pageData.Title, Description = pageData.Description, Content = pageData.PageContent, Keywords = pageData.Keywords, DateCreated = pageData.DateCreated.HasValue ? pageData.DateCreated.Value : DateTime.MinValue, DateModified = pageData.DateModified.HasValue ? pageData.DateModified.Value : DateTime.MinValue, IsPublished = pageData.IsPublished.HasValue ? pageData.IsPublished.Value : false, IsFrontPage = pageData.IsFrontPage.HasValue ? pageData.IsFrontPage.Value : false, Parent = pageData.Parent.HasValue ? pageData.Parent.Value : Guid.Empty, ShowInList = pageData.ShowInList.HasValue ? pageData.ShowInList.Value : false }; } } return page; }

 

 

 

Thanks,

 

Brian Davis

Java Blog

Coordinator
Dec 4, 2010 at 3:19 AM

Using LINQ alone (not LINQ to XML), you can use LINQ to query collections of data already available in BE, such as the Posts (or Pages, Categories, etc).

var thisYearPosts =
	from p in Post.Posts
	where p.DateCreated.Year == DateTime.Now.Year
	select p;

foreach (Post p in thisYearPosts)
{ 
}
If you wanted to use LINQ to XML, I think you would need to specify the physical .XML files you want to query.

Dec 4, 2010 at 1:26 PM

Thanks again BenAmada!

 

That helps alot :)