Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# “int”不包含“Select”的定义,并且找不到接受类型为“int”的第一个参数的扩展方法“Select”_C#_Linq_Selectlist - Fatal编程技术网

C# “int”不包含“Select”的定义,并且找不到接受类型为“int”的第一个参数的扩展方法“Select”

C# “int”不包含“Select”的定义,并且找不到接受类型为“int”的第一个参数的扩展方法“Select”,c#,linq,selectlist,C#,Linq,Selectlist,我有以下代码: using System.Data.Entity; using System.Diagnostics; using System.Linq; using System.Web.Mvc; using WebShopGoal.Models; namespace WebShopGoal.ViewModel { public class ProductEditViewModel { public Products Product { get; set;

我有以下代码:

using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Web.Mvc;
using WebShopGoal.Models;

namespace WebShopGoal.ViewModel
{
    public class ProductEditViewModel 
    {
        public Products Product { get; set; }
        public SelectList SupplierIdList { get; set; }
        public MultiSelectList CategoryIdList { get; set; }
        public int[] SelectedCategories { get; set; }
        public int? SupplierId { get; set; }

        private WebshopDBEntities _db; 

        public ProductEditViewModel()
        {
            _db = new WebshopDBEntities();

            SupplierIdList = new SelectList(_db.Suppliers, "Id", "Name");
            CategoryIdList = new MultiSelectList(_db.Categories, "Id", "CName");
        }

        public void Load(int id)
        {
            Product = _db.Products.Find(id);

            SupplierId = Product.SupplierId;
            SelectedCategories = Product.CategoryId.Select(p => p.Id).ToArray();    
        }
在最后一位,在select上,它给出了一个错误。我在一个不同的ViewModel中使用相同的代码,但它在那个模型中工作。 在我看来,我使用

<div class="form-group">
    @Html.LabelFor(model => model.Product.Categories, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.ListBoxFor(model => model.SelectedCategories, Model.CategoryIdList, htmlAttributes: new { @class = "form-control" })
    </div>
</div>
有人能帮我吗?这里有些搜索说使用System.Linq可以解决这个问题,但我已经找到了一个

我想你在这里要做的是:

SelectedCategories = new int[] { Product.CategoryId };

在我看来,你想要这个:

SelectedCategories = new int[] { Product.CategoryId };

只能对从IEnumerable派生的类型使用Linq。但是似乎Product.CategoryId属于int类型。我不确定您在这里尝试实现什么。您可以共享您的产品类吗?您可以发布您的产品类吗?在我看来,您需要此SelectedCategories=new int[]{Product.CategoryId};。创建表[dbo].[Products][Id][int]IDENTITY1,1不为NULL,[Name][varchar]max NULL,[Price][float]不为NULL,[Tax][int]不为NULL,[SupplierId][int]不为NULL,[CategoryId][int]不为NULL,--CaregoryId确实为int,难怪它不起作用他想要的是category Id而不是product Id;修好了