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);
}
IContactService から contactService のインスタンスを作成した後のコントローラで、これはフィルタリングとグループ化を行うコードです。
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);
}
この時点で、グループ化なしですべてのデータを取得します。
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();
504 件のレコードを取得する必要があります。グループを除いたすべてのデータである 523 件を取得します。プロファイラーを実行しましたが、実行されたステートメントに含まれるグループが表示されませんでした
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
どんなアイデアでも大歓迎です。
今のところ、最初にフィルターを発行し、データを取得して、結果が得られた後に groupby を実行することで作業を行いました。