开发者

One-to-one association and filtering in Linq

开发者 https://www.devze.com 2022-12-19 17:31 出处:网络
Let\'s say I have two tables (Address and Phone) in sql which have one-to-one relationship. I have created corresponding linq to sql classes and changed the association to OneToOne

Let's say I have two tables (Address and Phone) in sql which have one-to-one relationship. I have created corresponding linq to sql classes and changed the association to OneToOne

I want to retrieve both objects by filtering child object. e.g I have the following query which works fine:

var n = db.Addresses.Where(t => t.Phone.Number == 100);

Is there any way I can make the following work:

var n = db.Addresses.Where(t => t.Phone == new Phone(100));

The Phone class constructor above initializes the Number property. As I can see the query that is issued contains a clause which filters Phones table by id (primary key) but the number clause in not included.

If I set Number as primary key in visual studio then it is included in the where clause but the search still does not return anything as the parameter value for id is 0. Even if it worked it is not a solution as Number开发者_运维问答 should not be a primary key.


What you're asking doesn't quite make sense. If you want the phone element given the address, you have this by accessing the Address.Phone property.

You can't initialise a type and use that to project on as this is DLinq, where the query is translated down to the DB. You can do this on Linq to objects, but it would force you to enumerate your list, which would fetch all the database information into memory, which may not be good for performance...

I don't see the issue here? You have the address, a working search for it, and you can access the phone property by accessing the Address property.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号