2017-03-20 44 views
-5

嘿,人們提前感謝任何幫助。我是一名前端開發人員,但最近我一直在研究更多的全堆棧錯誤。我對C#很陌生,我想重構這個查詢來減少冗餘。我怎樣才能將這兩個linq查詢合併爲1,以便我只需要一個查詢。每個語句而不是兩個相同的查詢?我試過類似var query1 = xxx,然後query2 = query1.Where ...但我無法理解語法。再次感謝。結合linq查詢沒有多餘的代碼

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; 
    }   
} 
+2

你不能只是貼了大量的代碼,並要求其他人來修復它。谷歌一些教程,並開始工作。 – hellogoodnight

+1

你還沒有顯示出你的'Each'方法。你爲什麼不使用「foreach」?一般來說,這種方法「違反了所有其他序列操作符所基於的函數式編程原則,顯然調用這個方法的唯一目的是引起副作用。表達式的目的是計算一個值,而不是引起副作用,聲明的目的是引起副作用「_([」foreach「vs」ForEach「](https://blogs.msdn.microsoft.com/ericlippert/2009/05/18/ foreach-vs-foreach /)) –

+0

請重新格式化您的問題,以顯示數據的最低限度,可驗證的示例,linq查詢以及您嘗試將它們自己組合起來的問題,而且這個問題將進一步深入。 –

回答

-1

我解決了它。 @hellogoodnight你的答案特別有用。不管怎樣,謝謝你們...
命名空間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; 
    } 
} 

}