長時間潛伏者,最近的新人。動態查詢:在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」能夠解決它,但是,當然,這是不可能的,因爲這個變量是隱含類型的。
必須有申報我變量除了使用「無功」的方式...
如果任何人有任何想法,我會很感激。我希望我已經足夠簡明。
在此先感謝
哦,我的上帝,我甚至不知道這是可能的......實際上是這樣做的。 :) 非常感謝你! 我想我只是平了一些 – IndieRok 2013-05-08 13:28:31