简体   繁体   English

使用IQueryable select过滤列

[英]Using IQueryable select to filter columns

I've been exploring WEB API based on the guide at www.asp.net, and I've run into a problem when trying to filter columns from a table. 我一直在基于www.asp.net上的指南探索WEB API,并且在尝试从表中过滤列时遇到了问题。

My code is simply this: 我的代码就是这样:

    // GET api/Admin
    public IEnumerable<Product> GetProducts(testvar = "")
    {
        IQueryable<Product> productString = db.Products;

        if (testvar != "")
        {
            productString = productString.ToList().Select(i => new Product
            {
                Name = i.Name
            }).AsQueryable();
        }
        return productString;
    }

All I want to do is retrieve the Name column from the Product table for my JSON. 我要做的就是从JSON的“产品”表中检索“名称”列。 However, instead of getting something like: 但是,与其得到类似的信息:

[{"Name":"Hammer"}] or [{"$id":"1","Name":"Hammer"}]

I'm getting: 我越来越:

[{"$id":"1","Id":0,"Name":"Hammer","Price":0.0,"ActualCost":0.0}]

The Price and ActualCost values are normally 16.99 and 20.00, but instead of removing them, they are just returned as 0. Same for Id. Price和ActualCost值通常为16.99和20.00,但它们不会删除,而是返回为0。与ID相同。

Is there an easy way to stop it returning the Id, Price and ActualCost? 有没有一种简单的方法可以阻止它返回ID,价格和ActualCost? Perhaps I'm missing something really simple, but the below two links are the closest I've come to finding something that might work. 也许我缺少了一些非常简单的东西,但是下面的两个链接是我最近找到可能有用的东西的地方。

IQueryable C# Select IQueryable C#选择

The entity cannot be constructed in a LINQ to Entities query 无法在LINQ to Entities查询中构造实体

As per the guide I've been using ( http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-entity-framework/using-web-api-with-entity-framework,-part-1 ) I'm working in an MVC 4 project with the Orders, Projects and OrderDetails tables. 按照我一直在使用的指南( http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-entity-framework/using-web-api- with-entity-framework,-part-1 ),我正在使用Orders,Projects和OrderDetails表在MVC 4项目中工作。

Thanks 谢谢

You are still returning the full Product entity type, so even though you are projecting to a collection of new Product , the uninitialized properties will still have their default values (0 for int ). 您仍将返回完整的Product实体类型,因此,即使投影到新Product的集合,未初始化的属性仍将具有其默认值( int为0)。

You should create a new type which just contains the properties (eg ProductInfo ) you wish to expose and return a collection of this type. 您应该创建一个仅包含您希望公开的属性(例如ProductInfo )的新类型,并返回此类型的集合。

Alternatively, if your service supported the $select query option, the client could perform projections. 或者,如果您的服务支持$ select查询选项,则客户端可以执行投影。

I figured it out. 我想到了。 I just assigned the Product to a new variable and made the select call on that, and it worked perfectly. 我只是将Product分配给一个新变量,并对其进行了选择调用,它运行良好。

So now instead of: 所以现在代替:

productString = productString.ToList().Select(i => new Product
        {
            Name = i.Name
        }).AsQueryable();

I have 我有

var productInfos =
                    from i in productString
                    select new { i.Name };
                    newVariable.Add(productInfos);

I then return the newVariable, which is a list of objects instead of an IEnumerable of Products. 然后,我返回newVariable,它是对象列表,而不是产品的IEnumerable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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