Linq与不同列表中的实体列表

时间:2015-07-29 21:10:32

标签: c# sql linq entity-framework-6

我有一个SQL查询,它获取了不同的供应商联系人行,这些行返回了正确的行数:

SELECT  DISTINCT v.VendorID, c.ContactID, 
    v.VendorName, 
    c.FirstName, c.MiddleName, c.LastName,      
FROM VendorContacts vc
INNER JOIN Contact c
    ON c.ContactID = vc.ContactID
INNER JOIN Vendor v
    ON v.VendorID = vc.VendorID
LEFT JOIN [ContactServices] psvc
    ON psvc.ContactID = c.ContactID
    AND psvc.VendorID = v.VendorID

我有一个方法,我想根据上面的查询返回自定义类型:

public List<ProviderContactInfo> GetProviderContactInfo(ProviderContactInfo searchInfo)
{
    using (var db = new MyContext()) 
    {
        var providerContactInfo =
              (from vc in db.VendorContacts
               join ps in db.ContactServices on new { vc.ContactID, vc.VendorID } equals new { ps.ContactID, ps.VendorID } into ps_join
               from ps in ps_join.DefaultIfEmpty()
               join c in db.Contacts on vc.ContactID equals c.ContactID
               join v in db.Vendors on vc.VendorID equals v.VendorID                                           
               orderby vc.ContactID descending

               select new ProviderContactInfo()
               {
                VendorName = v.VendorName,
                FirstName = c.FirstName,
                MiddleName = c.MiddleName,
                LastName = c.LastName,
                Services = (from o in db.ContactServices
                            join cps in db.Contacts on o.ContactID equals cps.ContactID
                            join vps in db.Vendors on o.VendorID equals vps.VendorID
                            join s in db.Services on o.ServiceID equals s.ServiceID
                            where ps.ServiceID == o.ServiceID
                            && o.ContactID == c.ContactID
                            && o.VendorID == v.VendorID
                            select s).ToList()
               }).Distinct().ToList();

        return providerContactInfo;
    }
}

我收到了错误:

  

附加信息:“Distinct”操作无法应用于   指定参数的集合ResultType。

当我从新的ProviderContactInfo中删除Services属性时,一切正常,所以我确定它正在尝试填充该属性(类型为List<Service>

我知道有很多关于Linq with Distinct等的问题,但我找不到关于这个特定问题的任何内容。

请帮忙!

编辑此代码有效:

public List<ProviderContactInfo> GetProviderContactInfo(ProviderContactInfo searchInfo)
    {
        using (var db = new MyContext()) 
        {
            var providerContactInfo =
                  (from vc in db.VendorContacts
                   join c in db.Contacts on vc.ContactID equals c.ContactID
                   join v in db.Vendors on vc.VendorID equals v.VendorID                                           
                   orderby vc.ContactID descending

                   select new ProviderContactInfo()
                   {
                    VendorName = v.VendorName,
                    FirstName = c.FirstName,
                    MiddleName = c.MiddleName,
                    LastName = c.LastName,
                    Services = (from o in db.ContactServices
                                join cps in db.Contacts on o.ContactID equals cps.ContactID
                                join vps in db.Vendors on o.VendorID equals vps.VendorID
                                join s in db.Services on o.ServiceID equals s.ServiceID
                                where o.ContactID == c.ContactID
                                && o.VendorID == v.VendorID
                                select s).ToList()
                   }).Distinct().ToList();

            return providerContactInfo;
        }
    }

2 个答案:

答案 0 :(得分:1)

这不会更简单:

public IQueryable<VendorContact> GetProviderContactInfo(ProviderContactInfo searchInfo)
{
  using (var db = new MyContext()) 
  {
    return providerContactInfo=db.VendorContacts
      .Include(vc=>vc.Contacts)
      .Include(vc=>vc.Services)
      .Include(vc=>vc.Vendor)
      .OrderByDescending(vc=>vc.ContactID);
  }
}

答案 1 :(得分:1)

您应该向该过程添加一个步骤,以避免对具有集合(Distinct())属性的对象应用Services,LINQ提供程序不知道如何处理它。

var providerContactInfo = from vc in db.VendorContacts
                          join ps in db.ContactServices on new { vc.ContactID, vc.VendorID } equals new { ps.ContactID, ps.VendorID } into ps_join
                          from ps in ps_join.DefaultIfEmpty()
                          join c in db.Contacts on vc.ContactID equals c.ContactID
                          join v in db.Vendors on vc.VendorID equals v.VendorID                                           
                          orderby vc.ContactID descending
                          group ps by new
                          {
                              v.VendorName,
                              c.FirstName,
                              c.MiddleName,
                              c.LastName,
                              c.ContactID,
                              v.VendorID
                          } into g
                          select new ProviderContactInfo()
                          {
                              VendorName = g.Key.VendorName,
                              FirstName = g.Key.FirstName,
                              MiddleName = g.Key.MiddleName,
                              LastName = g.Key.LastName,
                              Services = (from e in g
                                          from o in db.ContactServices
                                          join cps in db.Contacts on o.ContactID equals cps.ContactID
                                          join vps in db.Vendors on o.VendorID equals vps.VendorID
                                          join s in db.Services on o.ServiceID equals s.ServiceID
                                          where e.ServiceID == o.ServiceID
                                          && o.ContactID == g.Key.ContactID
                                          && o.VendorID == g.Key.VendorID
                                          select s).ToList()
                          }