【问题标题】:Join with count and multiple conditions - LINQ C#加入计数和多个条件 - LINQ C#
【发布时间】:2014-05-14 06:55:39
【问题描述】:

我有一个属性数据库,我正在尝试获取用户添加的所有属性。主表称为“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 的“文件名”并获得计数时,我被卡住了。使用 cmets 参见下面的代码。

因此,对于上述数据,此查询应返回以下“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();
}

【问题讨论】:

    标签: c# linq join count multiple-conditions


    【解决方案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()
    

    【讨论】:

    • 感谢您的回复。如何在上述查询的选择中获取主图片(MainPic = 1)的“计数”和“文件名”
    • 这个 pp.PhotoLocation.Where(pic => pic.MainPic == true) 不起作用
    • 我可以让 MainPic 购买,在查询的 where 子句中添加条件 v.MainPic == true 但我如何获得计数?
    【解决方案2】:

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

    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();
    

    【讨论】:

      猜你喜欢
      • 2011-03-02
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      • 2014-08-03
      • 2019-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多