简体   繁体   English

如何在我的LINQ查询中包含第二个表中的列

[英]how to include a column from a second table in my LINQ query

I have a table called Ad (advertisement): 我有一个称为Ad (广告)的表:

public class Ad
{
    public long AdId { get; set; }
    public long UserId { get; set; } // <-- this is advertiser not currentUser
    public string Description { get; set; }
}

Users can add zero or more advertisements to their favourites list, so I have created another table called Favourite : 用户可以在其收藏夹列表中添加零个或多个广告,因此我创建了另一个名为Favourite表:

public class Favourite
{
    public long FavouriteId { get; set; }
    public long AdId { get; set; }
    public long UserId { get; set; }
}

And I have a table called User : 我有一个名为User的表:

public class User
{
    public long UserId { get; set; }
    public string Name { get; set; }
}

This is how I retrieve an ad, given currentUserId and adId : 给定currentUserIdadId ,这就是我检索广告的方式:

public TEntity GetAd(long currentUser, long adId)
{
    return Context.Ad
        .Where(r.AdId == adId)
        // include a flag indicating the pair (adId and currentUserId) exist in Favouries table
        .FirstOrDefault();
}

What I want to include in the result is a flag, indicating if the ad is added to the favourites list or not? 我想在结果中包含一个标志,指示是否将广告添加到收藏夹列表中?

In Entity Framework we have navigation properties. 在实体框架中,我们具有导航属性。 You can define navigation property on child table for the parent table row. 您可以在子表上为父表行定义导航属性。 For you case you can change your child entity like following: 对于您而言,您可以如下更改您的子实体:

 
 
 
  
  public class Favourite { public long FavouriteId { get; set; } public long AdId { get; set; } public long UserId { get; set; } public virtual Ad Ad { get; set; } }
 
  

and now the Entity Framework should take care of populating it for you and you can access the Ad related row of Favourite like : 现在,实体框架应负责为您填充它,您可以访问“ Favourite与“ Ad相关的行,例如:

return Context.Ad
        .Where(r.AdId == adId)
        Select(x => new 
                   { 
                      ad = x, 
                      IsFavourite = Context.Favourite.Any(y=> y.AdId = adId 
                                                    && y.UserId = currentUserid))
        .FirstOrDefault();

But in your case, You can write a query to know if the ad is favourite or not: 但就您而言,您可以编写查询以了解广告是否受到喜爱:

public UserFavouriteAd
{
    public Ad Ad { get; set; }
    public IsFavourite { get; set; }
}

you can create a Domain View Model and map it to that : 您可以创建一个域视图模型并将其映射到该模型:

return Context.Ad
            .Where(r.AdId == adId)
            .ToList()
            Select(x => new UserFavouriteAd 
                       { 
                          ad = x, 
                          IsFavourite = Context.Favourite.Any(y=> y.AdId = adId 
                                                        && y.UserId = currentUserid))
            .FirstOrDefault();

and populate it: 并填充它:

 return Context.Ad .Where(r.AdId == adId) .ToList() Select(x => new UserFavouriteAd { ad = x, IsFavourite = Context.Favourite.Any(y=> y.AdId = adId && y.UserId = currentUserid)) .FirstOrDefault(); 
public object GetAd(long currentUser, long adId)
{
    return from a in Context.Favourite
           where a.UserId  == currentUser
           select new {
                      flag = a.AdId != null
                      a.FavouriteId,
                      //etc
                      };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM