具有多对多和过滤功能的Linq查询或Lambda

时间:2019-06-11 20:18:03

标签: c# linq .net-core entity-framework-6

我有一些表User,UserSite,Site&Company。 UserSite是多对多关系,并且网站持有CompanyId。

我在创建Linq查询以选择带有用户站点和公司详细信息的用户时遇到了问题,这使此过程变得复杂的原因是正在进行大量筛选。您可以在网页上按用户,公司和站点进行过滤。

按站点搜索时,有必要在结果中显示该站点。但是,如果按用户搜索,我只想获取为该用户找到的第一个站点,因此删除重复的行。在T-SQL中,我将进行子联接并采用前1个,没问题。但是,对于Linq,我正在努力做到这一点。如此之多,我正在考虑回到Store Procs。

下面的代码由于加入了网站而为用户带回了多行,按网站过滤时如何为用户带回一行。

var users = from u in _arcContext.Users
join us in _arcContext.UserSites on u.Id equals us.UserId
join s in _arcContext.Sites on us.SiteId equals s.Id
join c in _arcContext.Companies on s.CompanyId equals c.Id
where 
    (!userSearchParams.UserId.HasValue || u.Id == userSearchParams.UserId) &&
    ((String.IsNullOrEmpty(userSearchParams.UserText) || userSearchParams.UserId.HasValue) || // User Text
        (u.FirstName.StartsWith(userSearchParams.UserText, StringComparison.InvariantCultureIgnoreCase)
            || u.LastName.StartsWith(userSearchParams.UserText, StringComparison.InvariantCultureIgnoreCase)
            || u.Username.StartsWith(userSearchParams.UserText, StringComparison.InvariantCultureIgnoreCase))
        ) &&
    ((!userSearchParams.CompanyId.HasValue || c.Id == userSearchParams.CompanyId)) && // Company Id
    ((String.IsNullOrEmpty(userSearchParams.CompanyText) || userSearchParams.CompanyId.HasValue) || // Company Text
        (c.Name.StartsWith(userSearchParams.CompanyText, StringComparison.InvariantCultureIgnoreCase)
            || c.Country.StartsWith(userSearchParams.CompanyText, StringComparison.InvariantCultureIgnoreCase)
            || c.City.StartsWith(userSearchParams.CompanyText, StringComparison.InvariantCultureIgnoreCase))
        ) &&
    ((!userSearchParams.SiteId.HasValue || s.Id == userSearchParams.SiteId)) && // Site Id
    ((String.IsNullOrEmpty(userSearchParams.SiteText) || userSearchParams.SiteId.HasValue) || // Site Text
        (s.Name.StartsWith(userSearchParams.SiteText, StringComparison.InvariantCultureIgnoreCase)
            || s.Country.StartsWith(userSearchParams.SiteText, StringComparison.InvariantCultureIgnoreCase)
            || s.City.StartsWith(userSearchParams.SiteText, StringComparison.InvariantCultureIgnoreCase)) 
        )
select new UserToSiteAndCompany()
{
User = u,
Site = s,
Company = c
};

更新1

 var site = (from u in _arcContext.Users
            join us in _arcContext.UserSites on u.Id equals us.UserId
            join s in _arcContext.Sites on us.SiteId equals s.Id
            where
                ((userSearchParams.SiteId.HasValue ? s.Id == userSearchParams.SiteId : true)) && // Site Id
                ((String.IsNullOrEmpty(userSearchParams.SiteText) || userSearchParams.SiteId.HasValue) || // Site Text
                    (s.Name.StartsWith(userSearchParams.SiteText, StringComparison.InvariantCultureIgnoreCase)
                        || s.Country.StartsWith(userSearchParams.SiteText, StringComparison.InvariantCultureIgnoreCase)
                        || s.City.StartsWith(userSearchParams.SiteText, StringComparison.InvariantCultureIgnoreCase)) 
                )
            select new { UserSite = us, Site = s}).Take(1);

            var users = from u in _arcContext.Users
            join us in _arcContext.UserSites on u.Id equals us.UserId
            join s in site on us.SiteId equals s.Site.Id 
            join c in _arcContext.Companies on s.Site.CompanyId equals c.Id
            where 
                (userSearchParams.UserId.HasValue ? u.Id == userSearchParams.UserId : true) &&
                ((String.IsNullOrEmpty(userSearchParams.UserText) || userSearchParams.UserId.HasValue) || // User Text
                    (u.FirstName.StartsWith(userSearchParams.UserText, StringComparison.InvariantCultureIgnoreCase)
                        || u.LastName.StartsWith(userSearchParams.UserText, StringComparison.InvariantCultureIgnoreCase)
                        || u.Username.StartsWith(userSearchParams.UserText, StringComparison.InvariantCultureIgnoreCase))
                    ) &&
                ((userSearchParams.CompanyId.HasValue ? c.Id == userSearchParams.CompanyId : true)) && // Company Id
                ((String.IsNullOrEmpty(userSearchParams.CompanyText) || userSearchParams.CompanyId.HasValue) || // Company Text
                    (c.Name.StartsWith(userSearchParams.CompanyText, StringComparison.InvariantCultureIgnoreCase)
                        || c.Country.StartsWith(userSearchParams.CompanyText, StringComparison.InvariantCultureIgnoreCase)
                        || c.City.StartsWith(userSearchParams.CompanyText, StringComparison.InvariantCultureIgnoreCase))
                    ) 
            select new UserToSiteAndCompany()
            {
            User = u,
            Site = s.Site,
            Company = c
            };

0 个答案:

没有答案