简体   繁体   中英

What is the correct syntax for returning IQueryable to a data-bound control

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!

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM