Examples of use of Entity Framework/LINQ

Jun 16, 2009 at 12:37 PM

Does anybody have any experience in loading QuickGraph objects with data from Entity Framework, or in using LINQ with QuickGraph?  Any sample code floating around? 

 

Coordinator
Jun 17, 2009 at 10:56 AM

Could you clarify what you have in mind? Are you looking to map the entity schema to a graph? All data structures in QuickGraph are generic, so you should be able to define TVertex as your entity and pick a default edge implementation such as Edge<T>, SEdge<T>, etc...

Jun 22, 2009 at 4:04 PM
Well, let me back up a bit.  I have two tables in a database: Vertices (which has an integer ID, and some attributes) and DirectedEdges (which has a FromVertex and ToVertex, as integer IDs).
 
I'm trying to find the most effective ways to get this data into a graph.  It occurred to me that some of the newer .NET 3.0/3.5/4.0 features (like LINQ, and EF4) might be useful in this regard, but I'd rather not reinvent the wheel, so I was wondering if anyone had played around in this arena. (It might well be that there are easier ways to load up the graph, using other technologies-- in which case, I'd be interested in hearing about those, too.)

On Wed, Jun 17, 2009 at 11:56 AM, pelikhan <notifications@codeplex.com> wrote:

From: pelikhan

Could you clarify what you have in mind? Are you looking to map the entity schema to a graph? All data structures in QuickGraph are generic, so you should be able to define TVertex as your entity and pick a default edge implementation such as Edge<T>, SEdge<T>, etc...

 
Coordinator
Jun 22, 2009 at 5:10 PM

You do not need linq to do this. You can either enumerate all the edges in your project and add them as you go in the graph (btw, do you plan to store the entities or just the IDs in the graph?).

Another way is to use the delegate-based graphs (you'll need to build from sources). These are graph implementation based on delegate, thus very flexible. You can instantiate a DelegateVertexAndEdgeListGraph and provide 2 delegates: one that returns a IEnumerable<TVertex>, and a delegate that returns out-edges given a vertex.

Jun 22, 2009 at 6:11 PM
The reason I thought LINQ might be attractive is that I have 59 million edges, so I was thinking of using LINQ to just return certain subsets instead of always dealing with the entire graph.
 
I wasn't sure about storing anything besides the IDs-- there are a few attributes of the vertices that might be useful, but there are 6 million vertices, so I'm trying to keep things fairly minimal.
 
Have you thought about adding some sample code to the site to demonstrate loading a graph from a database, and/or the use of delegate-based graphs?

On Mon, Jun 22, 2009 at 6:10 PM, pelikhan <notifications@codeplex.com> wrote:

From: pelikhan

You do not need linq to do this. You can either enumerate all the edges in your project and add them as you go in the graph (btw, do you plan to store the entities or just the IDs in the graph?).

Another way is to use the delegate-based graphs (you'll need to build from sources). These are graph implementation based on delegate, thus very flexible. You can instantiate a DelegateVertexAndEdgeListGraph and provide 2 delegates: one that returns a IEnumerable<TVertex>, and a delegate that returns out-edges given a vertex.

 
Coordinator
Jun 22, 2009 at 7:58 PM

Storing the IDs only and using SEquatableEdge<int> is a good idea in your case. Your code should look like this:

IEnumerable<int> vertexIDs = // create a query that select all vertices
    from v in db.Vertices select v.ID;
TryFunc<int, IEnumerable<SEquatable<int>> tryGetOutEdges = delegate(int id, out IEnumerable<SEquatable<int>> edges) {
    // query the database for the out edges of id 
    var query = from e in db.DirectedEdges
                          where e.SourceID == id
                          select new SEquatable<int>(e.SourceID, e.TargetID);
    // concretize the query
    var edgesArray = query.ToArray();
    edges = edgesArray;
    return edges.Length > 0;
    };
var g = new DelegateVertexAndEdgeListGraph(vertexIDs, tryGetOutEdges);

Jun 24, 2009 at 1:20 PM

Thanks for all the help, Peli-- it's much appreciated.

Unfortunately, I'm still having a few problems with the sample code you posted above.  I added the missing ">" to the TryFunc line, but I'm still getting two intellisense errors.

I'm told that 'Systems.Collection.Generics.IEnumerable<QuickGraph.SEquatableEdge<int>>' does not contain a definition for 'length' and no extension method 'length' accepting a first argument of type 'Systems.Collection.Generics.IEnumerable<Quickgraph.SEquatableEdge<int>>' could be found (Are you missingb a using directive or an assembly reference?)

Also, I get "Using the generic type 'QuickGraph.DelegateVertexAndEdgListGraph<TVertex,TEdge>' requires 2 type arguments."

Coordinator
Jun 24, 2009 at 5:17 PM

typo:

    return edgesArray.Length > 0;

C# should infer those generic type arguments. Maybe it does not because of the previous error. Otherwise, TVertex = int, TEdge = SEquatable<int>.

 

Jun 29, 2009 at 4:17 PM

I made the fixes that you suggested, and everything compiles fine.  I can build my graph, and see the Vertices.

Unfortunately, when I try to access g.OutEdges(myVertex) on a given vertex, I get the following exception:

System.ArgumentOutOfRangeException was unhandled
  Message=Specified argument was out of the range of valid values.
Parameter name: v
  Source=QuickGraph
  ParamName=v

Any ideas?

 

Coordinator
Jul 1, 2009 at 5:47 AM

Is 'myVertex' a valid vertex id?

Jul 1, 2009 at 8:55 AM
Sorry, I should have been more clear: I was actually trying to walk the graph.  My code was:

foreach (var vtx in g.Vertices) {

foreach (var ed in g.OutEdges(vtx)) {

Console.WriteLine(ed);
which followed after the sample code you provided to create the DelegateVertexAndEdgeListGraph.
On Wed, Jul 1, 2009 at 6:47 AM, pelikhan <notifications@codeplex.com> wrote:

From: pelikhan

Is 'myVertex' a valid vertex id?

Read the full discussion online.

To add a post to this discussion, reply to this email (quickgraph@discussions.codeplex.com)

To start a new discussion for this project, email quickgraph@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Coordinator
Jul 1, 2009 at 3:49 PM

Oops... I"ve made the impl of OutEdges more merciful. Sync or have the tryGetOutEdges method return true always.

Jul 1, 2009 at 4:38 PM
Thanks!  I synced, and it works great now.
 
Thanks again for all the help.

On Wed, Jul 1, 2009 at 4:49 PM, pelikhan <notifications@codeplex.com> wrote:

From: pelikhan

Oops... I"ve made the impl of OutEdges more merciful. Sync or have the tryGetOutEdges method return true always.

Read the full discussion online.

To add a post to this discussion, reply to this email (quickgraph@discussions.codeplex.com)

To start a new discussion for this project, email quickgraph@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com