潜伏已久,新人。
在使用 LINQ-to-Entities 进行查询时遇到了一些困难。我正在为 Web 和实体框架 4.0 使用 Visual Studio Express 2012。请耐心等待,这是一个很长的解释,以确保我涵盖了所有内容。情况如下:
我有一个可重复使用的 .aspx 页面,它充当图像管理器。它有一个Ajax Toolkit FileUpload 控件和一个Repeater 来显示上传的文件。我在 Fancybox 模态弹出窗口 (iframe) 中访问此管理器。它的打开 URL 有 2 个参数,一个“ID”和一个“类型”。
在我的 ImageManager 代码隐藏文件中,我有一个将所有图像绑定到转发器的函数。在这个函数中,我需要根据“类型”(url参数)查询一个数据库表......这正是问题所在。
对于我的生活,我不知道如何编写一个动态 LINQ-to-Entities 查询,其中表名会改变,根据类型改变......
这是函数(有问题的部分有注释):
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 查询”设为 NULL,那将解决它,但是,当然,这是不可能的,因为这个变量是隐式类型的。
除了使用“var”之外,必须有一种方法来声明我的变量......
如果有人有任何想法,我将不胜感激。我希望我足够简洁。
提前致谢