如何在 Linq 中选择在 GroupBy mothod 之后的新模型列表

本文关键字:新模型 列表 模型 mothod Linq 选择 GroupBy 之后 | 更新日期: 2024-11-08 03:22:02

我想选择分组的行到新的模型列表中。这是我的代码:

List<Model_Bulk> q = (from a in db.Advertises 
                      join c in db.Companies on a.AdvertiseCompanyID equals c.CompanyID
                      where a.AdvertiseActive == true 
                            && a.AdvertiseExpireDate.HasValue 
                            && a.AdvertiseExpireDate.Value > DateTime.Now 
                            && (a.AdvertiseObjectType == 1 
                                || a.AdvertiseObjectType == 2)
                      select c)
     .GroupBy(a => a.CompanyID).Select(a => new Model_Bulk
     {
         CompanyEmail = a.CompanyContactInfo.Email,
         CompanyID = a.CompanyID,
         CompanyName = a.CompanyName,
         Mobile = a.CompanyContactInfo.Cell,
         UserEmail = a.User1.Email,
         categories = a.ComapnyCategories
     }).ToList();

分组后,我无法使用 Select,自然会引发此语法错误:

System.Linq.IGrouping"不包含"CompanyContactInfo"的定义,也没有扩展方法"CompanyContactInfo"接受类型的第一个参数 可以找到System.Linq.IGrouping'(您是否缺少使用指令或程序集引用?

如果我尝试使用SelectMany()方法,但结果将重复并且分组方法无法正常工作:

List<Model_Bulk> q = (from a in db.Advertises
                      join c in db.Companies on a.AdvertiseCompanyID equals c.CompanyID
                      where a.AdvertiseActive == true 
                            && a.AdvertiseExpireDate.HasValue 
                            && a.AdvertiseExpireDate.Value > DateTime.Now 
                            && (a.AdvertiseObjectType == 1 
                                || a.AdvertiseObjectType == 2)
                      select c)
     .GroupBy(a => a.CompanyID).SelectMany(a => a).Select(a => new Model_Bulk
     {
         CompanyEmail = a.CompanyContactInfo.Email,
         CompanyID = a.CompanyID,
         CompanyName = a.CompanyName,
         Mobile = a.CompanyContactInfo.Cell,
         UserEmail = a.User1.Email,
         categories = a.ComapnyCategories
     }).ToList();

如何在 Linq 中选择在 GroupBy mothod 之后的新模型列表

您可以使用.Select(g => g.First())代替.SelectMany(a => a)。这将为您提供每个组的第一项。

(from a in db.Advertises
                          join c in db.Companies on a.AdvertiseCompanyID equals c.CompanyID
                          where a.AdvertiseActive == true && a.AdvertiseExpireDate.HasValue && a.AdvertiseExpireDate.Value > DateTime.Now && (a.AdvertiseObjectType == 1 || a.AdvertiseObjectType == 2)
                          select c)
             .GroupBy(a => a.CompanyID)
             .Select(g => g.First())
             .Select(a => new Model_Bulk
             {
                 CompanyEmail = a.CompanyContactInfo.Email,
                 CompanyID = a.CompanyID,
                 CompanyName = a.CompanyName,
                 Mobile = a.CompanyContactInfo.Cell,
                 UserEmail = a.User1.Email,
                 categories = a.ComapnyCategories
             }).ToList();

请注意,这可能不受支持,如果是这种情况,请在.Select(g => g.First())之前添加AsEnumerable调用

你应该明白,在 LinQ 表达式中执行GroupBy()后,你会与一个一起工作,所以在你的示例中,最好这样写:

List<Model_Bulk> q = 
    (from a in db.Advertises join c in db.Companies on a.AdvertiseCompanyID equals c.CompanyID                          
    where a.AdvertiseActive == true 
    && a.AdvertiseExpireDate.HasValue 
    && a.AdvertiseExpireDate.Value > DateTime.Now 
    && (a.AdvertiseObjectType == 1 || a.AdvertiseObjectType == 2)
    select c)
                 .GroupBy(a => a.CompanyID)
                 .Select(a => new Model_Bulk
                 {
                     CompanyEmail = a.First().CompanyContactInfo.Email,
                     CompanyID = a.Key, //Note this line, it's can be happened becouse of GroupBy()
                     CompanyName = a.First().CompanyName,
                     Mobile = a.First().CompanyContactInfo.Cell,
                     UserEmail = a.First().User1.Email,
                     categories = a.First().ComapnyCategories
                 }).ToList();

相反,您可以尝试这样的事情,而不是混合查询表达式和方法...(根据需要在 where/select 中使用 FirstOrDefault() )

(from a in db.Advertises
join c in db.Companies on a.AdvertiseCompanyID equals c.CompanyID
group a by new { a.CompanyId } into resultsSet
where resultsSet.AdvertiseActive == true && resultsSet.AdvertiseExpireDate.HasValue && resultsSet.AdvertiseExpireDate.Value > DateTime.Now && (resultsSet.AdvertiseObjectType == 1 || resultsSet.AdvertiseObjectType == 2)
select new Model_Bulk
{
    CompanyEmail = resultsSet.CompanyContactInfo.Email,
    CompanyID = resultsSet.CompanyID,
    CompanyName = resultsSet.CompanyName,
    Mobile = resultsSet.CompanyContactInfo.Cell,
    UserEmail = resultsSet.User1.Email,
    categories = resultsSet.ComapnyCategories
}).ToList();