1
public partial class ContactDetailView
{
    public virtual long LayerId { get; set; }
    public virtual long? AccountId { get; set; }
    public virtual long? UserId { get; set; }
    public virtual string LayerDescription { get; set; }
    public virtual long ContactId { get; set; }
    public virtual short CategoryTypeId { get; set; }
    public virtual string ContactCompany { get; set; }
    public virtual long ProfileId { get; set; }
    public virtual string ContactName { get; set; }
    public virtual short? TitleId { get; set; }
    public virtual string TitleDescription { get; set; }
    public virtual short? PhoneTypeId { get; set; }
    public virtual string PhoneNumber { get; set; } 
    public virtual short? EmailTypeId { get; set; }
    public virtual string EmailAddress { get; set; }
}

internal class ContactDetailViewMapping : EntityViewMappingBase<ContactDetailView>
{
    public ContactDetailViewMapping()
        : base()
    {
        HasKey(x => x.UserId).HasKey(x => x.LayerId).HasKey(x => x.ContactId);
        Property(x => x.LayerId).IsRequired();
        Property(x => x.AccountId).IsOptional();
        Property(x => x.UserId).IsOptional();
        Property(x => x.LayerDescription).IsRequired().HasMaxLength(250);
        Property(x => x.ContactId).IsRequired();
        Property(x => x.CategoryTypeId).IsRequired();
        Property(x => x.ContactCompany).IsOptional().HasMaxLength(250);
        Property(x => x.ProfileId).IsRequired();
        Property(x => x.ContactName).IsOptional().HasMaxLength(250);
        Property(x => x.TitleId).IsOptional();
        Property(x => x.TitleDescription).IsOptional().HasMaxLength(250);
        Property(x => x.PhoneTypeId).IsOptional();
        Property(x => x.PhoneNumber).IsOptional().HasMaxLength(15);
        Property(x => x.EmailTypeId).IsOptional();
        Property(x => x.EmailAddress).IsOptional().HasMaxLength(250);

        ToTable("vContactDetail");
    }
}

public interface IContactService : ICIBaseService<Contact>
{
    IQueryable<ContactDetailView> Search(long userId);
    IQueryable<ContactDetailView> ContactsDetail(long userId);
    Contact MatchContact(string firstName, string lastName, string emailAddress);
}

In the controller after I create an instance of the contactService from IContactService , this the code to do the filtering and grouping:

        var query = contactService.ContactsDetail(UserContext.UserId);
        if (model.CategoryId > 0)
        {
            query = query.Where(c => c.CategoryTypeId == model.CategoryId);
        } 
        if (model.LayerId > 0)
        {
            query = query.Where(c => c.LayerId == model.LayerId);
        }

AT this point I get back all the data without the grouping:

        var result = (from contact in contactService.ContactsDetail(UserContext.UserId)
                    group contact by new
                        {
                            ContactId = contact.ContactId,
                            AccountId = contact.AccountId,
                            UserId = contact.UserId,
                            CategoryTypeId = contact.CategoryTypeId,
                            ContactCompany = contact.ContactCompany,
                            ProfileId = contact.ProfileId,
                            ContactName = contact.ContactName,
                            PhoneTypeId = contact.PhoneTypeId,
                            PhoneNumber = contact.PhoneNumber,
                            EmailTypeId = contact.EmailTypeId,
                            EmailAddress = contact.EmailAddress,
                            TitleId = contact.TitleId,
                            TitleDescription = contact.TitleDescription
                        }
                        into groupedContact
                        select new ContactSearchResultModel
                            {
                                ContactId = groupedContact.Key.ContactId,
                                ContactName = groupedContact.Key.ContactName,
                                Title = groupedContact.Key.TitleDescription,
                                Company = groupedContact.Key.ContactCompany,
                                BusinessEmail = groupedContact.Key.EmailAddress,
                                WorkPhone = groupedContact.Key.PhoneNumber
                            }

                            ).OrderBy(c => c.ContactName)
                            .ToList();

I should get back 504 rescords I get back 523, all the data without the group. I ran profiler and did not see the group by included in the executed statement

exec sp_executesql N'SELECT 
[Project1].[C1] AS [C1], 
[Project1].[ContactId] AS [ContactId], 
[Project1].[ContactName] AS [ContactName], 
[Project1].[TitleDescription] AS [TitleDescription], 
[Project1].[ContactCompany] AS [ContactCompany], 
[Project1].[EmailAddress] AS [EmailAddress], 
[Project1].[PhoneNumber] AS [PhoneNumber]  
FROM ( SELECT 
[Extent1].[ContactId] AS [ContactId], 
[Extent1].[ContactCompany] AS [ContactCompany], 
[Extent1].[ContactName] AS [ContactName], 
[Extent1].[TitleDescription] AS [TitleDescription], 
[Extent1].[PhoneNumber] AS [PhoneNumber], 
[Extent1].[EmailAddress] AS [EmailAddress], 
1 AS [C1]
FROM [dbo].[vContactDetail] AS [Extent1]
WHERE [Extent1].[UserId] = @p__linq__0
)  AS [Project1]
ORDER BY [Project1].[ContactName] ASC',N'@p__linq__0 bigint',@p__linq__0=2

Any ideas is greatly appreciated.

For now I made work by issuing the filter first, retrieving the data and doing the groupby after I get the result.

4

0 回答 0