2013-05-07 157 views
2

長時間潛伏者,最近的新人。動態查詢:在LINQ-to-Entities查詢中更改表名稱

一直在用LINQ-to-Entities進行查詢。我正在使用Visual Studio Express 2012 for Web和Entity Framework 4.0。忍受我,這是一個長期的解釋,確保我覆蓋一切。這裏的情況:

我有一個可重用的.aspx頁面充當圖像管理器。它有一個Ajax Toolkit FileUpload控件,還有一個Repeater來顯示上傳的文件。我在Fancybox Modal Pop-up(iframe)中訪問此管理器。它的開放網址有2個參數,一個「ID」和一個「類型」。

在我的ImageManager代碼隱藏文件中,我有一個函數將所有圖像綁定到中繼器。在這個函數中,我需要根據「類型」(url參數)來查詢數據庫表......這正是問題所在。

對於我的生活,我不知道該怎麼寫一個動態的LINQ到實體查詢,其中表名稱將改變,根據更改的類型......

這裏是功能(有問題的部分有意見):

private void BindImagesRepeater() 
{ 
    DatabaseEntities db = new DatabaseEntities(); 
    FolderName = Session["foldername"].ToString(); 
    FolderPath = Server.MapPath("~") + "controls/" + FolderName + "/images"; 
    int itemid = Convert.ToInt32(Session["itemID"]); 
    bool dirExist = Directory.Exists(@FolderPath + "/" + itemid); 
    bool isSpotlight = false; 
    string spotlightThumbPath = ""; 
    string thumbPath = ""; 
    string thumbname = ""; 
    string spotlightName = ""; 
    int thumbPosition = 0; 
    int spotlightPosition = 0; 
    int counter = 0; 
    int rptrCount = 0; 

    /////PART I DON'T QUITE UNDERSTAND 
    var query = null; //I know this is impossible 

    //select the Table to query 
    switch (FolderName) 
    { 
     case "NewsManager": 
      query = (from q in db.News Where q.ID == itemID select q); 
      break; 
     case "ProductsManager": 
      query = (from q in db.Products Where ... 
      break; 
     case "ProjectsManager": 
      query = ... db.Projects ... 
      break; 
     case "CalendarManager": 
      query = ... db.Events ... 
      break; 
    } 
    ///// 


    if (query.Count > 0) 
    { 
     var uniqueItm = query.First(); 

     isSpotlight = Convert.ToBoolean(uniqueItm.isSpotlight); 
     thumbPath = uniqueItm.thumbnailPath; 
     spotlightThumbPath = uniqueItm.spotlightThumbPath; 

     thumbname = Path.GetFileNameWithoutExtension(thumbPath); 
     spotlightName = Path.GetFileNameWithoutExtension(spotlightThumbPath); 

     if (dirExist) 
     { 
      if (!String.IsNullOrWhiteSpace(FolderPath)) 
      { 
       List<string> fileNames = GetFiles(FolderPath + "/" + itemid, "*.bmp|*.gif|*.jpg|*.jpeg|*.png", SearchOption.TopDirectoryOnly); 
       var files = (from f in fileNames 
          where (!f.Contains("_Thumb")) && (!f.Contains("_SpotlightThumb")) 
          select new 
          { 
           FilePath = f, 
           FileName = System.IO.Path.GetFileName(f) 
          }); 

       if (files.Count() > 0) 
       { 
        imagesRepeater.DataSource = files; 
        imagesRepeater.DataBind(); 

        foreach (var img in files) 
        { 

         if (thumbPath != "" && thumbPath != null) 
         { 
          if (img.FileName.Contains(thumbname.Replace("_Thumb", ""))) 
          { 
           thumbPosition = counter; 
          } 
         } 

         if (spotlightThumbPath != "" && spotlightThumbPath != null) 
         { 
          if (img.FileName.Contains(spotlightName.Replace("_SpotlightThumb", ""))) 
          { 
           spotlightPosition = counter; 
          } 
         } 

         counter++; 
        } 

        if (isSpotlight) 
        { 
         foreach (RepeaterItem item in imagesRepeater.Items) 
         { 
          LinkButton spotlightLnkBtn = (LinkButton)item.FindControl("useAsThumbnailSpotlight"); 
          spotlightLnkBtn.Visible = true; 

          if (thumbPath != "" && thumbPath != null) 
          { 
           if (rptrCount == thumbPosition) 
           { 
            Label myThumbImage = (Label)item.FindControl("lblThumb"); 
            myThumbImage.Visible = true; 
           } 
          } 

          if (spotlightThumbPath != "" && spotlightThumbPath != null) 
          { 
           if (rptrCount == spotlightPosition) 
           { 
            Label mySpotlightImage = (Label)item.FindControl("lblThumbSpotlight"); 
            mySpotlightImage.Visible = true; 
           } 
          } 

          rptrCount++; 
         } 
        } 
        else 
        { 
         foreach (RepeaterItem item in imagesRepeater.Items) 
         { 
          if (thumbPath != "" && thumbPath != null) 
          { 
           if (rptrCount == thumbPosition) 
           { 
            Label myThumbImage = (Label)item.FindControl("lblThumb"); 
            myThumbImage.Visible = true; 
           } 
          } 

          if (spotlightThumbPath != "" && spotlightThumbPath != null) 
          { 
           if (rptrCount == spotlightPosition) 
           { 
            Label mySpotlightImage = (Label)item.FindControl("lblThumbSpotlight"); 
            mySpotlightImage.Visible = true; 
           } 
          } 

          rptrCount++; 
         } 
        } 

        uniqueItm.hasImages = true; 
        db.SaveChanges(); 
        lblEmptyData.Visible = false; 
       } 
       else 
       { 
        uniqueItm.hasImages = false; 
        db.SaveChanges(); 
        lblEmptyData.Visible = true; 
       } 
      } 
     } 
    } 
    else 
    { 
     lblEmptyData.Visible = true; 
    } 
} 

我嘗試了幾個不同的東西,這是無濟於事。我很難過。 如果我可以讓我的「var query」能夠解決它,但是,當然,這是不可能的,因爲這個變量是隱含類型的。

必須有申報我變量除了使用「無功」的方式...

如果任何人有任何想法,我會很感激。我希望我已經足夠簡明。

在此先感謝

回答

3

通過看你如何使用您的查詢,似乎你的結果是強類型 ...

var uniqueItm = query.First(); 

    isSpotlight = Convert.ToBoolean(uniqueItm.isSpotlight); 
    thumbPath = uniqueItm.thumbnailPath; 
    spotlightThumbPath = uniqueItm.spotlightThumbPath; 

每個查詢應該返回結果有isSpotlight,thumbnailPath,spotlightThumbPath性能。 那麼,爲什麼不爲創建類:

public class MyQueryResult 
{ 
    public bool isSpotlight{get;set;} 
    public string thumbPath{get;set;} 
    public string spotlightThumbPat{get;set;} 
} 

和類型的所有查詢返回的MyQueryResult

IQueryable<MyQueryResult> query = null; //This is now possible 
//select the Table to query 
switch (FolderName) 
{ 
    case "NewsManager": 
     query = from q in db.News Where q.ID == itemID 
       select new MyQueryResult 
       { 
        isSpotLight=q.Something, 
        thumbPath = q.SomethingElse 
        etc... 
        } 
     break; 
    case "ProductsManager": 
     query = (from q in db.Products Where ... 
       select new MyQueryResult 
       { 
        ---fill properties 
       } 
     break; 
    .... same thing for every case 
    } 

    var uniqueItm = query.FirstOrDefault(); 
    if (uniqueItm!=null) 
    { 
     ... do your thing with uniqueItm 
    } 
+1

哦,我的上帝,我甚至不知道這是可能的......實際上是這樣做的。 :) 非常感謝你! 我想我只是平了一些 – IndieRok 2013-05-08 13:28:31

0

其實我可以有我的頭頂上有一個解決方案:

注:該解決方案沒有經過測試。

第一:

在你的情況下,var result,就像你說的,是隱式類型。但實際上它相當於IEnumerable<object> result。您可以使用IEnumerable<object> result = nullIEnumerable<object­>? result來接受可爲空的值。

第二:

第二種解決方案將包括查詢部分移動到其自己的方法,接受字符串參數,返回的對象集合或可空的IEnumerable<T>?集合。

問候!

+0

另一種解決方案解決我的問題的對象,但您的解決方案是有道理也。我會在以後記住這一點。感謝您花時間尋找解決方案。 – IndieRok 2013-05-08 13:34:35