Pergunta

let's say I have a pesistent class called DailyVisitorSummary, which describes for each web page how many visitors it had per day. For simplicitzy, assume that we represent the day as a pure integer.

Now I would like to create a query to retrieve a specific day data, and also the data from its previous and next days. What I know is that there surely wil be at most one previous and next day data record for the same webpage, so I could write an SQL query (MySQL syntax) like:

SELECT c.*,p.*,n.* from DailyVisitorSummary c
LEFT JOIN DailyVisitorSummary p ON p.WebPage = c.WebPage AND p.Day = c.Day - 1
LEFT JOIN DailyVisitorSummary n ON n.WebPage = c.WebPage AND n.Day = c.Day + 1
WHERE c.Day = 453;

I would like to populate the following viewmodel with the result:

public class VMDailyVisitors3Day {
   public VMDailyVisitors CurrentDay { get; set; }
   public VMDailyVisitors PreviousDay { get; set; }
   public VMDailyVisitors NextDay { get; set; }
}

public class VMDailyVisitors {
    public int Day { get; set;; }
    public int WebPageID { get; set; }
    public int VisitorCount { get; set; }
}

How could I do this query with Linq to XPO? I need a LINQ solution, because I need to use the result in a server-mode MVC GridView.

Foi útil?

Solução 2

I've found a workaround with the help of the DX support by using the PersistentAliasAttribute as a middle step, where I can do free joins, and then use that property in the XPQuery. If anyone intrested, check out here.

Outras dicas

Linq to XPO supports group join only. A possible solution would be to create a SQL view in the database and populate it with data using your SQL query. Then you can map another persistent class to this view to obtain data.

The view must have at least one column with unique values. Do not use the newid function or similar to generate unique values, because this approach assigns different values to one and the same row each time the data is being queried. Server Mode uses a key column to identify rows. Use actual data to populate a key column. For example, concatenate values from the WebPage and the Day columns. Make sure that this produces distinct values, though.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top