我正在爲Windows Phone(SDK 7.1)應用程序編寫Silverlight,並且正在從Windows Phone Silverlight工具包的LongListSelector
控件中顯示來自CompactSQL DB的數據。一旦列表變得大約150個項目,應用程序真的減緩加載數據,導航到頁面和動畫無法顯示(我知道使用後臺線程將有助於釋放UI線程的動畫)。將Linq2SQL簡單查詢轉換爲CompiledQueries以提高應用程序性能
我目前有三個查詢,我經常使用 - 每次從LongListSelector更新數據或頁面NavigatedTo。我已經將MoviesByTitle
轉換爲CompiledQuery
,這有很大的幫助,所以我期待爲我的其他兩個查詢(groupedMovies
和LongListSelector.ItemSource
,List<Group<Movie>>
)做同樣的事情,但我似乎無法弄清楚正確的語法。
有關如何使這些查詢更有效的任何建議 - 通過使用CompiledQuery
或其他方式?
MoviesByTitle
是在稱爲Queries
public static Func<MovieDBContext, IOrderedQueryable<Movies>> MoviesByTitle = CompiledQuery.Compile((MovieDBContext db) => from m in db.Movies orderby m.Title,m.Year select m);
字段中的MainPage
private static List<String> characters = new List<String> { "#", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
public static List<Group<Movies>> emptyGroups = new List<Group<Movies>>();
在的MainPage一個LOADDB()方法內的另一個類 - 調用此方法中的OnNavigatedTo並且在其它幾個地方時數據庫已更新。
//Populates the 'empty' Groups of Movies objects only once.
if (emptyGroups.Count < 1)
{
characters.ForEach(x => emptyGroups.Add(new Group<Movies>(x, new List<Movies>())));
}
IEnumerable<Movies> query = Queries.MoviesByTitle(App.db);
//Groups the objects
IEnumerable<Group<Movies>> groupedMovies = (from t in query
group t by t.GroupHeader into grp
orderby grp.Key
select new Group<Movies>(grp.Key.ToString(), grp));
//Joins the 'empty' group and groupedMovies together for the LongListSelector control
moviesLongList.ItemsSource = (from t in groupedMovies.AsEnumerable().Union(emptyGroups)
orderby t.Title
select t).ToList();
GroupHeader
是Movies
在DB
[Column(CanBeNull=true, UpdateCheck = UpdateCheck.Never)]
public char GroupHeader
{
get
{
char l;
//For Alphabetized Grouping, titles do NOT start with "The ..."
if (this.Title.ToLowerInvariant().StartsWith("the "))
{
l = this.Title.ToLowerInvariant()[4];
}
else
{
l = this.Title.ToLower()[0];
}
if (l >= 'a' && l <= 'z')
{
return l;
}
return '#';
}
set { }
}
的Group
類的屬性和一個實體如下
public class Group<T> : IEnumerable<T>
{
public Group(string name, IEnumerable<T> items)
{
this.Title = name;
this.Items = new List<T>(items);
}
public string Title
{
get;
set;
}
public IList<T> Items
{
get;
set;
}
...
}
感謝這一切。特別是對於解釋 - 從長遠來看,知識將幫助我更直接的代碼。 我用秒錶來測試我的代碼,然後實施一些你需要優化的建議。 – Styff 2012-04-16 14:12:37