嘿伙计们提前感谢任何帮助。我是一名前端开发人员,但最近我一直在研究更多的全栈错误。我对C#很陌生,我想重构这个查询以减少冗余。如何将这两个linq查询合并为1,这样我只需要1个查询。每个语句而不是两个相同的语句?我尝试了类似var query1 = xxx然后query2 = query1.Where ...但是我无法正确获得语法。再次感谢。
public interface ILoginLinkService
{
Dictionary<string, IList<NameValuePair>> GetLoginPageLinks();
}
public class LoginLinkService : ILoginLinkService
{
private readonly IUnitOfWork unitOfWork;
public LoginLinkService(IUnitOfWork unitofwork)
{
this.unitOfWork = unitofwork;
}
public Dictionary<string, IList<NameValuePair>> GetLoginPageLinks()
{
var data = new Dictionary<string, IList<NameValuePair>>();
var query = (from s in unitOfWork.Repository<LoginPageLink>().Queryable()
orderby s.LoginPageLinkCategory.Code, s.SortOrder
select new
{
s.LoginPageLinkCategory.Code,
s.Name,
s.Url,
});
query.Each(x =>
{
var pair = new NameValuePair() {Name = x.Name, Value = x.Url,};
IList<NameValuePair> list;
if (data.ContainsKey(x.Code))
{
list = data[x.Code];
}
else
{
list = new List<NameValuePair>();
data[x.Code] = list;
}
list.Add(pair);
});
var announcementsQuery = (from s in unitOfWork.Repository<LoginPageLink>().Queryable()
.Where(x => x.LoginPageLinkCategory.Code == LoginPageLinkCategory.AnnouncementsCode &&
(DbFunctions.DiffDays(x.CreatedDate, DateTimeOffset.Now) ?? 0) <= 7)
select new
{
s.LoginPageLinkCategory.Code,
s.Name,
s.Url,
});
announcementsQuery.Each(x =>
{
var pair = new NameValuePair() { Name = x.Name, Value = x.Url, };
IList<NameValuePair> list;
if (data.ContainsKey(x.Code))
{
list = data[x.Code];
}
else
{
list = new List<NameValuePair>();
data[x.Code] = list;
}
list.Add(pair);
});
return data;
}
}
答案 0 :(得分:-1)
我解决了。 @hellogoodnight你的回答特别有帮助。无论如何,谢谢你们... namespace xxx.Business.Common.Services {
public interface ILoginLinkService
{
Dictionary<string, IList<NameValuePair>> GetLoginPageLinks();
}
public class LoginLinkService : ILoginLinkService
{
private readonly IUnitOfWork unitOfWork;
public LoginLinkService(IUnitOfWork unitofwork)
{
this.unitOfWork = unitofwork;
}
public Dictionary<string, IList<NameValuePair>> GetLoginPageLinks()
{
var data = new Dictionary<string, IList<NameValuePair>>();
var baseQuery = unitOfWork.Repository<LoginPageLink>().Queryable();
var nonAnnouncementsQuery = baseQuery
.Where(x => x.LoginPageLinkCategory.Code != LoginPageLinkCategory.AnnouncementsCode)
.Select(x => new
{
x.LoginPageLinkCategory.Code,
x.Name,
x.Url,
x.SortOrder
});
var announcementsQuery = baseQuery
.Where(x => x.LoginPageLinkCategory.Code == LoginPageLinkCategory.AnnouncementsCode &&
(DbFunctions.DiffDays(x.CreatedDate, DateTimeOffset.Now) ?? 0) <= 7)
.Select(x => new
{
x.LoginPageLinkCategory.Code,
x.Name,
x.Url,
x.SortOrder
});
var fullQuery = nonAnnouncementsQuery.Union(announcementsQuery)
.OrderBy(x => new { x.Code, x.SortOrder })
.Select(x => new
{
x.Code,
x.Name,
x.Url
});
fullQuery.Each(x =>
{
var pair = new NameValuePair() { Name = x.Name, Value = x.Url, };
IList<NameValuePair> list;
if (data.ContainsKey(x.Code))
{
list = data[x.Code];
}
else
{
list = new List<NameValuePair>();
data[x.Code] = list;
}
list.Add(pair);
});
return data;
}
}
}