Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MVC和亚音速pagedlist创建自定义可查询对象时出错_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 2 - Fatal编程技术网

C# 使用MVC和亚音速pagedlist创建自定义可查询对象时出错

C# 使用MVC和亚音速pagedlist创建自定义可查询对象时出错,c#,asp.net,asp.net-mvc,asp.net-mvc-2,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 2,你好,我有下面的代码,但是当我尝试创建一个新的IQuerable时,我得到一个错误,接口无法实现,如果我去掉新的,我会得到一个未实现的异常,在过去的一个月里,我不得不跳回一些旧的ASP经典站点,在我的一生中,我无法唤醒我的大脑进入C#模式 请您看一下下面的内容,并给我一些关于我哪里出了问题的线索: 代码是创建priceItems的列表,但是我将以字符串形式显示名称,而不是categoryID(int) public ActionResult ViewPriceItems(int?

你好,我有下面的代码,但是当我尝试创建一个新的IQuerable时,我得到一个错误,接口无法实现,如果我去掉新的,我会得到一个未实现的异常,在过去的一个月里,我不得不跳回一些旧的ASP经典站点,在我的一生中,我无法唤醒我的大脑进入C#模式

请您看一下下面的内容,并给我一些关于我哪里出了问题的线索:

代码是创建priceItems的列表,但是我将以字符串形式显示名称,而不是categoryID(int)

        public ActionResult ViewPriceItems(int? page)
    {
        var crm = 0;
        page = GetPage(page);

        // try and create items2
        IQueryable<ViewPriceItemsModel> items2 = new IQueryable<ViewPriceItemsModel>();

        // the data to be paged,but unmodified
        var olditems = PriceItem.All().OrderBy(x => x.PriceItemID);

        foreach (var item in olditems)
        {
            // set category as the name not the ID for easier reading
            items2.Concat(new [] {new ViewPriceItemsModel {ID = item.PriceItemID,
                  Name = item.PriceItem_Name,
                  Category = PriceCategory.SingleOrDefault(
                       x => x.PriceCategoryID == item.PriceItem_PriceCategory_ID).PriceCategory_Name,
                  Display = item.PriceItems_DisplayMethod}});
        }


        crm = olditems.Count() / MaxResultsPerPage;
        ViewData["numtpages"] = crm;
        ViewData["curtpage"] = page + 1;

        // return a paged result set
        return View(new PagedList<ViewPriceItemsModel>(items2, page ?? 0, MaxResultsPerPage));
    } 
public ActionResult视图价格项目(int?页)
{
var-crm=0;
page=GetPage(第页);
//尝试创建项目2
IQueryable items2=新IQueryable();
//要分页但未修改的数据
var olditems=PriceItem.All().OrderBy(x=>x.PriceItemID);
foreach(旧项目中的var项目)
{
//将类别设置为名称而不是ID,以便于阅读
items2.Concat(新[]{new ViewPriceItemsModel{ID=item.PriceItemID,
Name=item.PriceItem\u Name,
类别=PriceCategory.SingleOrDefault(
x=>x.PriceCategoryID==item.PriceItem\u PriceCategory\u ID).PriceCategory\u名称,
Display=item.PriceItems\u DisplayMethod});
}
crm=olditems.Count()/MaxResultsPerPage;
ViewData[“numtpages”]=crm;
ViewData[“curtpage”]=第+1页;
//返回分页结果集
返回视图(新页面列表(items2,第0页,MaxResultsPerPage));
} 

非常感谢

您不需要创建项目2。删除带有注释的行try and create items2。使用以下代码。我还没有测试过这个。但我希望这能奏效

var items2 = (from item in olditems
                      select new ViewPriceItemsModel
                          {
                              ID = item.PriceItemID,
                              Name = item.PriceItem_Name,
                              Category = PriceCategory.SingleOrDefault(
                                 x => x.PriceCategoryID == item.PriceItem_PriceCategory_ID).PriceCategory_Name,
                              Display = item.PriceItems_DisplayMethod
                          }).AsQueryable();

您不需要创建项目2。删除带有注释的行try and create items2。使用以下代码。我还没有测试过这个。但我希望这能奏效

var items2 = (from item in olditems
                      select new ViewPriceItemsModel
                          {
                              ID = item.PriceItemID,
                              Name = item.PriceItem_Name,
                              Category = PriceCategory.SingleOrDefault(
                                 x => x.PriceCategoryID == item.PriceItem_PriceCategory_ID).PriceCategory_Name,
                              Display = item.PriceItems_DisplayMethod
                          }).AsQueryable();

感谢您的响应,但这不会创建一个可查询对象,只是一个ViewPriceItemsModel感谢您的响应,但这不会创建一个可查询对象,只是一个ViewPriceItemsModel