我有这个方法试图得到一个事项列表:
private static IQueryable<Thing> GetThings(int thingsType)
{
try
{
return from thing in entities.thing.Include("thingStuff")
select thing;
}
catch (Exception exception)
{
return new EnumerableQuery<Thing>(?????);
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我不能以任何理由让查询运行,我想返回一个空的IQueryable.我不想返回NULL,因为这可能会破坏调用代码.是否可能或者我对此完全错了?
小智 50
这些答案很好并且可以正常工作,但是我总是觉得使用Empty而不是创建一个新的List更清洁:
Enumerable.Empty<Thing>().AsQueryable();
Run Code Online (Sandbox Code Playgroud)
请尝试以下方法:
private static IQueryable<Thing> GetThings(int thingsType)
{
IQueryable<Thing> things = new List<Thing>().AsQueryable();
try
{
things = from thing in entities.thing.Include("thingStuff")
select thing;
return things;
}
catch (Exception exception)
{
return things;
}
}
Run Code Online (Sandbox Code Playgroud)
我会添加块finally {}并将我的返回类型放在该代码中.
这将通过返回应用程序所期望的类型来解决问题.
private static IQueryable<T> GetThings(int thingsType)
{
IQueryable<T> list = new List<Thing>().AsQueryable();
try
{
list = from thing in entities.thing.Include("thingStuff")
select t;
}
catch (Exception exception)
{
// handle exception here;
}
finally {
return list;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10468 次 |
| 最近记录: |