Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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# Web API控制器内的自定义属性_C#_Asp.net_Asp.net Web Api - Fatal编程技术网

C# Web API控制器内的自定义属性

C# Web API控制器内的自定义属性,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,我在WebAPI控制器中有如下内容: [EnableQuery()] public IQueryable<ProductDTO> Get() { var products = from p in db.Products select new ProductDTO() { Id = p.Id, Created = p.Created, Title = p.Title,

我在WebAPI控制器中有如下内容:

[EnableQuery()]
public IQueryable<ProductDTO> Get()
{
    var products = from p in db.Products
        select new ProductDTO()
        {
            Id = p.Id,
            Created = p.Created,
            Title = p.Title,
            CustomValue = ? (if Id=1 or 2, then CustomValue = 1, if Id=3, then CustomValue = 2 etc)
        };

    return products.AsQueryable();
}
[EnableQuery()]
公共IQueryable Get()
{
var products=以db.products表示的p
选择新产品dto()
{
Id=p.Id,
已创建=p.已创建,
标题=p.标题,
CustomValue=?(如果Id=1或2,则CustomValue=1,如果Id=3,则CustomValue=2等)
};
退货产品。AsQueryable();
}
我想基于另一个属性的值返回CustomValue。在本例中,Id为。因此,如果Id为1或2,CustomValue应返回1,如果Id为3,CustomValue应返回2,依此类推。
我如何才能做到这一点?

这与web api或控制器无关

public IQueryable<ProductDTO> Get()
{
    var products = from p in db.Products
        select new ProductDTO()
        {
            Id = p.Id,
            Created = p.Created,
            Title = p.Title,
            CustomValue = p.Id == 1 || p.Id == 2 ? 1 : p.Id == 3 ? 2 :0;
        };

    return products.AsQueryable();
}
public IQueryable Get()
{
var products=以db.products表示的p
选择新产品dto()
{
Id=p.Id,
已创建=p.已创建,
标题=p.标题,
CustomValue=p.Id==1 | | p.Id==2?1:p.Id==3?2:0;
};
退货产品。AsQueryable();
}