[英]C# : how to declare a variable for an entity table?
我正在使用 C# 实体框架到数据库表中的 select 记录。 根据选择标准,我将使用不同的 select 语句。
if ( condition1 )
{
var records = _context.Member.Select( .... )
}
else if (condition2)
{
var records = _context.Member.Select( .....)
}......
然后,我需要根据是否有记录做出一些决定并处理这些记录。
if (records != null)
{
}
else if (....)
编译器抱怨"records" does not exist in the current context
。 我认为发生这种情况的原因是记录是在 if 块中声明的。 我不想在 if 块中执行第二步,因为处理是相同的并且很长。
我试过先在块外声明记录,但我不知道要使用什么类型。 那么如何声明一个变量来保存从实体框架返回的记录呢?
谢谢。
什么数据类型是记录? 找出它并调用它。如果您使用的是 Visual Studio,只需 hover 超过 Select 方法。 它会弹出一些关于方法的信息,在方法名称前还有一个返回类型。
然后编写这段代码:
T records = null; // or some kind of empty collection
if ( condition1 )
{
records = _context.Member.Select( .... )
}
else if (condition2)
{
records = _context.Member.Select( .....)
}
您遇到此问题的原因是“记录”仅在“{}”花括号的 scope 中定义。 通过像我向您展示的那样提出它,您可以将其移动到您做出决定的同一环境中
if (records != null)
{
}
else if (....)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.