开发者

LINQ to SQL query help

I 开发者_运维问答have 2 entities -Offer and Share. Share stores the offers shared by users.

class Offer
    int IDOffer;
    bool Shared;
    (other properties)

class Share
    int IDOffer;
    int IDUser;

where Offer.IDOffer = Share.IDOffer, 1-to-1 (users may share an offer only once, or not at all).

I extended Offer with the Shared property -it's not in the database but in the entity.

Now I need to select Offers and set Offers.Shared based on the Share entity.

Could you point me the right way? I appreciate your help.


I don't believe these sort of properties are supported well in LINQ to SQL. It seems you should be able to write something like this:

void partial OnLoaded()
{
    this.Shared = !this.Share == null;
}

But I think this won't work because the OnLoaded method may execute before your entity is actually loaded. I recommend just comparing Share to null whenever you need this sort of idea expressed. Like this:

//Select all offers that are shared
var result  = from offer in Offers
              where !offer.share == null
              select offer;

Edit: Wrote the first version forgetting this is a one-to-one. Edited to work with EntityRefs.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜