加入计数和多个条件 - LINQ C#

时间:2014-04-02 19:10:14

标签: c# linq join count multiple-conditions

我有一个属性数据库,我试图让用户添加所有属性。主表名为' Property'还有其他表格是“PropertyPhotos'”' City'等样本数据库如下:

'属性'表

PropertyId| Area| State| UserId | ...
1         | 1   | 1    | AAA    | ...
2         | 2   | 3    | BBB    | ... 
3         | 1   | 1    | AAA    | ...

' PropertyPhotos'

PropertyPhotoId| PropertyId| FileName  | MainPic 
1              | 1         | x1.jpg    | 1
2              | 1         | X2.jpg    | 0
3              | 2         | x3.jpg    | 1
4              | 3         | x4.jpg    | 1
5              | 3         | x5.jpg    | 0
6              | 3         | x6.jpg    | 0

' AreaLookUp'

AreaLookUpId | AreaDescription
    1        | London
    2        | Birmingham
    3        | Manchester

我正在尝试编写LINQ查询以获取特定用户添加的属性信息。但是在尝试检索' FileName' MainPic也得到了统计。请参阅下面的代码和评论。

因此,对于上面的数据,此查询应返回" UserId = AAA"

的以下内容
PropertyId | ... | MainPicSrc | PhotoCount
    1      | ... | x1.jpg     | 2
    3      | ... | xr4jpg     | 3

请帮忙!

public IEnumerable<PropertyExcerptViewModel> GetAddedPropertyVmByUserId(string userId) 
{
    var addedProperties = from p in db.Property where p.UserId == userId
                          join pp in db.PropertyPhotos on p.PropertyId equals pp.PropertyId
                          join a in db.AreaLookUp on p.Area equals a.AreaLookUpId
                          select new PropertyExcerptViewModel
                          {
                              PropertyId = p.PropertyId,
                              PropertyType = p.PropertyType,
                              TransactionType = p.TransactionType,
                              IsPropertyDisabled = p.IsPropertyDisabled,
                              IsPropertyVerified = p.IsPropertyVerified,
                              IsPropertyNotified = p.IsPropertyNotified,
                              MainPicSrc = pp.FileName, // How to put where condition to only get FileName of just the Main Pic 
                              PhotoCount = pp.Count(), // How to get count of all pics with a particular proprtyId
                              Price = p.Price,
                              NoOfBedrooms = p.NoOfBedrooms,
                              Area = a.AreaLookUpDescription,
                              ShortDescription = (p.Description.Length > 300) ? p.Description.Substring(0,300) : p.Description
                          };

    return addedProperties.ToList();
}

2 个答案:

答案 0 :(得分:1)

如果您关心明确匹配,我认为哪种陈述可能更容易

    var data=(from c in db.Property from v in db.PropertyPhotos from 
    n in db.AreaLookUpId 
    where c.PropertyId==v.PropertyId && c.Area==n.AreaLookUpId && c.UserId=="AAA"
 // the rest is your select

PhotoCount = v.Where(j=>j. PropertyId==c.PropertyId).Count()

答案 1 :(得分:0)

这也有效 - 我最终这样做了

var addedProperties = from p in db.Property
                                  join ppic in db.PropertyPhotos on p.PropertyId equals ppic.PropertyId into pp
                                  join a in db.AreaLookUp on p.Area equals a.AreaLookUpId
                                  join cal in db.CalendarEvent on p.PropertyId equals cal.PropertyId into c
                                  where p.UserId == userId
                                  select new PropertyExcerptViewModel
                                  {
                                      PropertyId = p.PropertyId,
                                      PropertyType = p.PropertyType,
                                      PropertyCategoryDescription = pc.PropertyCategoryDescription,
                                      TransactionType = p.TransactionType,
                                      IsPropertyDisabled = p.IsPropertyDisabled,
                                      IsPropertyVerified = p.IsPropertyVerified,
                                      IsPropertyNotified = p.IsPropertyNotified,
                                      MainPicSrc = pp.Where(e => e.MainPic == true).FirstOrDefault().PhotoLocation,
                                      PhotosCount = pp.Count(),
                                      Price = p.Price,
                                      NoOfBedrooms = p.NoOfBedrooms,
                                      Area = a.AreaLookUpDescription,
                                      ShortDescription = (p.Description.Length > 300) ? p.Description.Substring(0, 300) : p.Description,
                                      LatestCalendarEvent = c.OrderByDescending(e => e.DateSaved).FirstOrDefault()
                                  };

            return addedProperties.ToList();