C# - LINQ查询 - 保持击球抓 [英] c# - Linq Query - Keeps hitting catch
问题描述
我是新的C#,所以希望我可以给你在这里的足够信息。
我试图返回的产品,在产品的状态列中值为1的列表。
不过,我一直在打我一抓,并返回null。
我想为每一个产品的清单,但我还没有得到那么远,目前刚刚倾倒的结果变成可视数据。
我在catach获得唯一的例外是:
的无效操作...:不能按类型'Davey.Models.GetAllProducts订单的
下面是我的code:
我的模型:(产品型号)
命名空间Davey.Models
{
[DataContract]
公共类GetAllProducts
{
[数据成员]
公共字符串ID {搞定;组; }
[数据成员]
公共字符串名称{;组; }
[数据成员]
公共字符串说明{搞定;组; }
[数据成员]
公共字符串价格{搞定;组; }
[数据成员]
公共字符串点{搞定;组; }
[数据成员]
公共字符串猫{搞定;组; }
[数据成员]
公共字符串大小{搞定;组; }
[数据成员]
公共字符串SizeM {搞定;组; }
[数据成员]
公共字符串{SIZEL获得;组; }
[数据成员]
公共字符串SizeXL {搞定;组; }
[数据成员]
公众诠释状态{搞定;组; }
}
}
我的服务(DaveyService.scs)://这是我不停的按catch块
公共GetAllProducts [] AllProducts(INT状态)
{
尝试
{
使用(UserDataDataContext DB =新UserDataDataContext())
{
返回db.mrobProducts.Where(X => x.Status ==状态)。选择(X =>新建GetAllProducts {名称= x.Name,说明= x.Description,价格= x.Price})排序依据( X =&X的催化剂).ToArray();
}
}
抓住
{
返回null;
}
}
和我的控制器,它已经被砍倒在相关部分:
[HttpPost]
[使用AllowAnonymous]
公众的ActionResult用户登陆(LoginModel模型,用户名usernameModel,GetAllProducts allProductsModel)
{
如果(userExists)
{
VAR webService3 =新DaveyServiceClient();
allProductsModel.Status = 1;
计算机[产品] = webService.AllProducts(allProductsModel.Status);
返回视图(../私人/索引);
}
返回查看(「指数」);
}
的问题是,你的排序依据
方法不知道如何通过 GetAllProducers
。
您有两种选择:
- 实施
IComparable的< GetAllProducts>
在GetAllProducts
-
排序东西
排序依据
将知道如何进行排序,如字符串
或INT
,例如:使用(UserDataDataContext DB =新UserDataDataContext())
{
返回db.mrobProducts.Where(X => x.Status ==状态)。选择(X =>新建GetAllProducts
{
名称= x.Name,
说明= x.Description,
价格= x.Price
})的OrderBy(X => x.Price).ToArray();
}
,我通常喜欢指定我抓什么IM捕
子句,你应该总是做一些与你捕捉异常,即使它只是日志记录。我建议你使用特定类型的如赶上(InvalidOperationException异常E)
或者如果你真的需要一个常规catch那么赶上(例外五)
I'm new to c#, so hopefully I can give you enough information here.
I am trying to return a list of products, where the product status column has a value of 1.
But, I keep hitting a my catch, and returning null.
I would like to create a list for each product, but I have not even got that far yet, currently just dumping the results into a viewdata.
The exception I am getting on the catach is: Invalid operation...:Cannot order by type 'Davey.Models.GetAllProducts'.
Here is my code:
My Model: (Product Model)
namespace Davey.Models
{
[DataContract]
public class GetAllProducts
{
[DataMember]
public string ID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Desc { get; set; }
[DataMember]
public string Price { get; set; }
[DataMember]
public string Points { get; set; }
[DataMember]
public string Cat { get; set; }
[DataMember]
public string SizeS { get; set; }
[DataMember]
public string SizeM { get; set; }
[DataMember]
public string SizeL { get; set; }
[DataMember]
public string SizeXL { get; set; }
[DataMember]
public int Status { get; set; }
}
}
My Service (DaveyService.scs): // This is where I keep hitting the catch block
public GetAllProducts[] AllProducts(int status)
{
try
{
using (UserDataDataContext db = new UserDataDataContext())
{
return db.mrobProducts.Where(x => x.Status == status).Select(x => new GetAllProducts { Name = x.Name, Desc = x.Description, Price = x.Price }).OrderBy(x => x).ToArray();
}
}
catch
{
return null;
}
}
And my controller, which has been cut down to the relevant part:
[HttpPost]
[AllowAnonymous]
public ActionResult UserLogin(LoginModel model, UserName usernameModel, GetAllProducts allProductsModel)
{
if (userExists)
{
var webService3 = new DaveyServiceClient();
allProductsModel.Status = 1;
ViewData["products"] = webService.AllProducts(allProductsModel.Status);
return View("../Private/Index");
}
return View("Index");
}
The problem is that your OrderBy
method doesn't know how to sort by GetAllProducers
.
You have two choices:
- Implement
IComparable<GetAllProducts>
inGetAllProducts
Sort by something
OrderBy
will know how to sort by, like astring
or anint
, for example:using (UserDataDataContext db = new UserDataDataContext()) { return db.mrobProducts.Where(x => x.Status == status).Select(x => new GetAllProducts { Name = x.Name, Desc = x.Description, Price = x.Price }).OrderBy(x => x.Price).ToArray(); }
As a side note, i usually like to specify what im catching in my catch
clause, as you should always do something with the exception you catch, even if its just logging. I suggest you use a specific type like catch (InvalidOperationException e)
or if you really need a general catch then catch (Exception e)
这篇关于C# - LINQ查询 - 保持击球抓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!