views:

246

answers:

3

I have 3 arrays coming from a webservice...

Countries : Consists of Key - Value
Regions   : Consists of Id - Name - CountryCode(fk: countries)
Areas     : Consists of Id - Name - CountryCode - RegionID(fk: regions)

(the fk is just showing that its the bit of information, foreign key, associating it to the previous array)

I'm stuck on what the best way (most optimal) is to link these together, LINQ Joining looks like a headache and I'm not sure about HashSets.

Any Ideas?

*** Additional;* This will be stored in the application state as the APP as it needs to be refreshed once a day (which in my opinion is quicker to do by simply refreshing the app once a day than storing in a database and updating based on a date stamp.

A: 

Linq joins really aren't that bad, the syntax is a little funny at first, but you get used to it pretty quickly.

Matthew Vines
+5  A: 

I'm curious why you say that LINQ Joining looks like a headache: it is exactly what LINQ to Objects is for (querying your in-memory objects and providing services such as joins between those objects).

A potential downside in LINQ is performance overhead, but to be honest it hasn't turned up as a major problem for me yet.

The syntax is simply from obj1 in source1 join obj2 in source2 on <conditional> select {obj1.prop, obj2.prop}

This mirrors SQL pretty heavily, which was the intent.

(To use your example:

from a in Areas
join r in Regions 
 on a.RegionID equals r.Id
join c in Countries 
 on r.CountryCode equals  c.Key
select new {c.Value, r.Name, a.Name, a.Id };

)

Godeke
Your right that doesn't look quite as bad as I expected.
Chris M
To cache this for later use, you'll want to define a flattened type that encompasses those properties and wrap the LINQ in `(...).ToList()` so it is not lazily executed each time it is requested.
Jason
Thanks to both of you :o)
Chris M
Jason is correct as to a way to avoid a fairly mild performance penalty. Anonymous types such as created here with new { } work well enough, and frankly when typing madly in a browser window I rarely bother with custom classes :-P
Godeke
@Godeke:I'm right in thinking == should be equals?var commonRAR = from a in dAreas join r in dRegions on a.RegionID equals r.Id join c in dCountries on r.CountryCode equals c.Key select new {c.Value, r.Name, a.Name, a.Id };Going off - http://msdn.microsoft.com/en-us/library/bb384085.aspx
Chris M
Yeah, I'm used to using LLBLGen which lets me get away with that via an overload. Corrected in the body.
Godeke
+1  A: 

Can't you dump them into a DataSet? It's a collection of DataTables that you can predefine Relations for.

private void CreateRelation() 
{
    // Get the DataColumn objects from two DataTable objects 
    // in a DataSet. Code to get the DataSet not shown here.
    DataColumn parentColumn = 
        DataSet1.Tables["Customers"].Columns["CustID"];
    DataColumn childColumn = 
        DataSet1.Tables["Orders"].Columns["CustID"];
    // Create DataRelation.
    DataRelation relCustOrder;
    relCustOrder = new DataRelation("CustomersOrders", 
        parentColumn, childColumn);
    // Add the relation to the DataSet.
    DataSet1.Relations.Add(relCustOrder);
}
ian_scho
talk about memory intensive....
Stan R.