0

我有一个属性数据库,我正在尝试获取用户添加的所有属性。主表称为“Property”,还有其他表是“PropertyPhotos”、“City”等。示例数据库如下:

“属性”表

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

'物业照片'

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

'区域查找'

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

我正在尝试编写一个 LINQ 查询来获取有关特定用户添加的属性的信息。但是当我试图检索 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();
}
4

2 回答 2

1

我认为如果您关心明确匹配, where 声明可能会更容易

    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()
于 2014-04-02T19:17:39.867 回答
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();
于 2014-04-02T21:07:58.470 回答