将 IQueryable 返回到数据绑定控件的正确语法是什么

发布于 2024-12-05 03:29:53 字数 1108 浏览 1 评论 0原文

我正在进行一些重构以消除 SqlDataSource 控件。下面是我的业务逻辑类中的代码,旨在返回给定 ID 的图像列表。

public class BusinessLogic
{        
    public IQueryable<SBMData2.File> GetPackageImages(int id)
    {
        SBMData2.SBMDataContext db = new SBMData2.SBMDataContext();

        IQueryable<SBMData2.File> PackageImages = from f in db.Files
                                                  where f.PackageID == id
                                                  select f;
        return PackageImages;

    }
}

在我后面的代码中,我尝试通过执行类似以下操作将 IQueryable 对象绑定到 asp 转发器。

int id = RadGrid1.SelectedValues["id"].ToString();
Repeater1.DataSource = BusinessLogic.GetPackageImages(id);
Repeater1.DataBind();

我的项目模板包含一个简单的图像标签,用于从绑定的 IQueryable 对象中绑定 FileName。例如:

<img src="PackageFile.ashx?Thumb=true&Id=<%# DataBinder.Eval(Container.DataItem, "FileName").ToString() %>" alt="loading image ..."/>

我的问题有两个部分。首先,我在 BusinessLogic 类中的语法是否正确?我怀疑这不是因为我无法从后面的代码访问 GetPackageImages。其次,我几乎可以肯定我没有正确指定数据源。我该如何纠正这个问题?

谢谢你!

I'm doing some refactoring to eliminate SqlDataSource controls. Below is code from my business logic class that is intended to return a list of images for a given ID.

public class BusinessLogic
{        
    public IQueryable<SBMData2.File> GetPackageImages(int id)
    {
        SBMData2.SBMDataContext db = new SBMData2.SBMDataContext();

        IQueryable<SBMData2.File> PackageImages = from f in db.Files
                                                  where f.PackageID == id
                                                  select f;
        return PackageImages;

    }
}

In my code behind I'm trying to bind the IQueryable object to an asp repeater by doing something like the following.

int id = RadGrid1.SelectedValues["id"].ToString();
Repeater1.DataSource = BusinessLogic.GetPackageImages(id);
Repeater1.DataBind();

My item template contains a simple image tag that binds FileName from the bound IQueryable object. For example:

<img src="PackageFile.ashx?Thumb=true&Id=<%# DataBinder.Eval(Container.DataItem, "FileName").ToString() %>" alt="loading image ..."/>

My question has two parts. First, is my syntax correct inside of the BusinessLogic class? I suspect it is not because I cannot access GetPackageImages from my code behind. Second, I am almost certain that I did not specify my datasource correctly. How can I correct that?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

财迷小姐 2024-12-12 03:29:53

您已将其声明为实例方法,但您试图将其作为静态方法获取,除非您有一个名为BusinessLogic的属性 类型 BusinessLogic... 尝试将其设为静态:

public static IQueryable<SBMData2.File> GetPackageImages(int id)

我不太了解数据绑定,但看起来可能没问题。

就我个人而言,我不会在 GetPackageImages 中使用查询表达式,而且我也不会使用 PascalCase 命名局部变量,请注意。我只想用:

SBMData2.SBMDataContext db = new SBMData2.SBMDataContext();
return db.Files.Where(f => f.PackageID == id);

You've declared it as an instance method, but you're trying to get at it as a static method, unless you've got a property called BusinessLogic of type BusinessLogic... Try just making it static:

public static IQueryable<SBMData2.File> GetPackageImages(int id)

I don't know much about data binding, but it looks like it could be okay.

Personally I wouldn't use a query expression in GetPackageImages, and I also wouldn't name local variables with PascalCase, mind you. I'd just use:

SBMData2.SBMDataContext db = new SBMData2.SBMDataContext();
return db.Files.Where(f => f.PackageID == id);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文