views:

602

answers:

2

Hi,

Can I use partial classes to create properties that points to an association property generated by the L2S designer. Also, will I be able to use the new property in queries?

How can I achieve this?

+2  A: 

If you just want to give a different name to the association property, just use the Property page for the association and rename the parent and/or child property. That will change the name of the EntityRef/EntitySet in the class.

EDIT: The downside of using a separate property in a partial class is that LINQ won't be able to use it when generating queries -- essentially you'll be forced to always get the entities before you can use the related properties on the object. By renaming you allow LINQ to use the related properties in constructing the query which can result in a more efficient query. For example, if you want to get entities where a related entity has a particular property value, using the attribute decorated entity will allow LINQ to generate the SQL to pull just those matching values from the database. With the naive property implementation (that simply references the underlying relation property, in effect renaming it), you will be forced to first get all entities, then do the filtering in your application.

tvanfosson
This is not a practical approach. The changes are overridden when I regenerate the model :(
Ali Kazmi
+1 it is a practical approach. Linq2sql doesn't have a regenerate model button, doing changes in the designer is part of working with it. That said will add an answer for more info on your question.
eglasius
By regenerate, do you mean remove the table and add it back in? Yes, that is a problem, but surely that doesn't happen very often. Or are you using an external mapping file? I don't have any experience with that.
tvanfosson
You can reference the loaded properties, but if you attempt to select on a value of one of the properties in the indirect reference, it won't be able to translate it directly to SQL.
tvanfosson
see my update on one of the tests I ran.
eglasius
Try adding a where clause "where d.MyTestData.ID == 1" or something like that.
tvanfosson
...sorry wrong wording in my comment. I didn't mean "selection" I meant the where predicate that does the selection based on the child/parent property.
tvanfosson
+1  A: 

Yes you can, but you have to apply the same attributes as the linq2sql generated property i.e.

 [Association(Name="Test_TestData", Storage="_TestDatas", ThisKey="SomeId", OtherKey="OtherId")]
    public System.Data.Linq.EntitySet<TestData> MyTestDatas
    {
        get
        {
            return this.TestDatas;
        }
    }

TestDatas being the original relation.

Update: A sample query I ran:

        var context = new DataClasses1DataContext();
        var tests =
            from d in context.Tests
            where d.MyTestDatas.Any(md=>md.MyId == 2)
            select new
            {
                SomeId = d.SomeId,
                SomeData = d.SomeData,
                Tests = d.MyTestDatas
            };
        foreach (var test in tests)
        {
            var data = test.Tests.ToList();
        }
eglasius
This won't work the same way if the parent/child relation is used in queries since the indirect property won't be supported for expression translation.
tvanfosson
I just ran 2 sample queries :)
eglasius
@Freddy -- you can reference the loaded properties, but I don't think it will work for selection's. Without the attribute decoration I don't think it has the hints required to generate the expression.
tvanfosson
@Freddy -- see my correction on my answer -- I mean in the where clause.
tvanfosson
Correct, it won't work for where clauses, in which case the only way would be to replicate the attributes.
eglasius
@tvanfosson thx, updated my answer based on that. In the samples I ran the extra pieces were run after the initial query, which doesn't happen with normal relation properties. Somehow I though it had to do with entity sets or something special ... back on the right track now :)
eglasius