1

EntityFrameworkを使用してデータベースからデータを取得しています。ここでデータベースモデルを見ることができます:

edmx

リソースがある場合にのみリソースカテゴリを表示するリピーターを作成しようとしていwhere IsProtected == falseます。次に、リソース自体を表示するネストされたリピーター。

これは私が探しているものを明確にするのを助けるために省略されたリピーターです

<asp:Repeater>
    <h2>Category Name</h2>
    <ol>
        <asp:Repeater DataSource="<%# ((ResourceCategory)Container.DataItem).Resource %>">
            <li>Resource Name</li>
        </asp:Repeater>
    </ol>
</asp:Repeater>

私が現在使用しているクエリは、を含むすべてのカテゴリをプルアップしますが、実際にはテーブルに関連しているResource.Count() > 0ため、ステートメントの記述方法がわかりません。whereResource

public List<Model.ResourceCategory> GetResourcesWithDocuments()
{
    using (var context = new SafetyInSightEntities())
    {
        return (from cat in context.ResourceCategory.Include("Resource")
                orderby cat.Name
                where cat.Resource.Count > 0
                select cat).ToList();
    }
}

誰かが私のLINQクエリを書き直して、私の内部リピーターがリソースを表示するのを手伝ってくれませんか?IsProtected == false

4

2 に答える 2

2

あなたが何を求めているのか正確にはわかりませんが、これが欲しいと思いますか?

public List<Model.ResourceCategory> GetResourcesWithDocuments()
{
    using (var context = new SafetyInSightEntities())
    {
        return (from cat in context.ResourceCategory
                orderby cat.Name
                where cat.Resource.Any()
                select new 
                {
                    CategoryName = cat.Name,
                    Resources = cat.Resource.Where(r => r.IsProtected == false).ToList()
                }).ToList();
    }
}

Allではなくを使用したい可能性がありますAnyが、質問に基づいているかどうかはわかりません。

于 2013-02-26T03:22:04.973 に答える
1

これを試して:

public IEnumerable<ResourceCategoryFacade> GetResourcesWithDocuments() // return weaker interface
{
    using (var context = new SafetyInSightEntities())
    {
        var q = from cat in context.ResourceCategory.Include(cat => cat.Resource)
                orderby cat.Name
                select new ResourceCategoryFacade // anonymous, or compiler-time type
                {
                    CategoryName = cat.Name,
                    Resources = cat.Resource.Where(r => !r.IsProtected).ToList() // or ToArray()
                };
        return q.ToList(); // or ToArray()
    }
}
于 2013-02-26T03:35:33.330 に答える