如何返回此IQueryable查询? [英] How do I return this IQueryable query?

查看:273
本文介绍了如何返回此IQueryable查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有这个实体查询,我想捕获结果,以便我可以在if else语句或任何地方使用它。 />


Hi,

I have this entity query and I want to capture the result so that I can use it in an if else statement or where ever.

private AH_ODS_DB_Entities db = new AH_ODS_DB_Entities();
IQueryable rs = db.Transmitals;
                    rs = db.Transmitals
                            .Select (sl => new Transmital { Serial = sl.Serial })
                            .Where  (sl => sl.Serial == 888)
                            .AsQueryable();





我看待它的方式,它可以执行查询但它返回一个对象。我想把结果放在一个变量中,这样我就可以在if else语句或将来的目的中使用它。



顺便说一句,这是一个post方法。我将从邮递员的原始Json帖子中比较这个结果。



编辑:

序列号是传输表中的一列。



The way I look at it, it can execute the query but it returns an object. I want to put the result in a variable so I can use it in an if else statement or future purposes.

This is in a post method by the way. I am going to be comparing this result from a raw Json post from postman.


Serials is a column in the Transmital table.

推荐答案

这会得到一个新的()传输对象的列表,就像您在已发布的代码中创建了新对象:

This would get you a list of new (!) "Transmital"-objects, just like you created new objects in your posted code:
private AH_ODS_DB_Entities db = new AH_ODS_DB_Entities();
List<Transmital> rs = db.Transmitals
                        .Where  (sl => sl.Serial == 888)
                        .Select (sl => new Transmital { Serial = sl.Serial })
                        .ToList();





当然你必须知道这是否是你需要的 - 对我而言看起来像它可能是错误的。返回原始对象的查询将是:



Of course you would have to know if that's what you need - to me it looks like it might be by mistake. A query that returns the "original" objects would be this:

private AH_ODS_DB_Entities db = new AH_ODS_DB_Entities();
List<Transmital> rs = db.Transmitals
                        .Where  (sl => sl.Serial == 888)
                        .ToList();





然后你可以使用 rs 在一些if语句中。如果您想要检查if语句是否有任何匹配的传输,那么您可以使用返回bool的查询:



And then you could use rs in some if-statement. If all you would want to check in your if-statement was if there are any matching "Transmitals" then you could use a query that returns a bool instead:

private AH_ODS_DB_Entities db = new AH_ODS_DB_Entities();
bool anyMatchingTransmitals = db.Transmitals
                                .Where  (sl => sl.Serial == 888)
                                .Any();





同样你可以使用.Count()而不是.Any()如果​​你对匹配的传输感兴趣(返回类型是 int 而不是 bool 然后)。



这有帮助吗?



Likewise you could use .Count() instead of .Any() if you're interested in the amount of matching "Transmitals" (the return type would be int instead of bool then).

Does this help?


这篇关于如何返回此IQueryable查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆