使用LINQ从Sql加载完整图 [英] Loading Complete graph from Sql using LINQ

查看:60
本文介绍了使用LINQ从Sql加载完整图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个Menu类,其中包含SubMenus(与Menu的类型相同,因此也可以具有SubMenus和Items)和Items(具有不同的类型),然后将它们存储在两个表中(一个用于Menus,另一个用于Tables)用于项目). 有什么方法可以使用LINQ to SQL加载Menu类的完整图(包括其所有的SubMenus和Items)?

Assume we have a Menu class that has SubMenus (which is the same type of Menu therefore can have SubMenus and Items too) and Items (which has a different type) and we stored them in two tables(One for Menus and one for Items). Is there any way to load complete graph of the Menu class (all of its SubMenus and Items) using LINQ to SQL?

推荐答案

如果您在SQL中正确定义了关系,则LINQToSQL应该通过对菜单的单个查询就为您提供所有菜单/项目.但是,这不会使它重新用作图形,而是作为菜单(带有相关菜单和项目)的枚举.您需要遍历此枚举并自己构造图.

If you define the relationships properly in SQL, LINQToSQL should give you back all of the menus/items with a single query on menus. However, this won't give it back to use as a graph, but as an enumeration of the menus (with related menus and items). You would need to iterate through this enumeration and construct the graph on your own.

另一方面,您可能可以执行以下操作:

On the other hand, you might be able to do something like this:

var roots = db.Menus.Where( m => !m.SubMenus.Count == 0 );

然后,roots将是基本菜单的集合.如果您不打算在其上执行任何图形分析算法,而仅打算从上到下遍历以构造GUI组件,则这可能就足够了.

Then, roots would be a collection of the base menus. This might be sufficient if you don't intend to perform any graph analysis algorithms on it, but only intend to traverse it from top to bottom to construct the GUI components.

这篇关于使用LINQ从Sql加载完整图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆