我有一个对象列表,其中包含以下对象:
<servlet>
<servlet-name>SpringDispatcherServer</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springConfig.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDispatcherServer</servlet-name>
<url-pattern>/test/api/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>AuthCheck</filter-name>
<filter-class>org.test.util.AuthCheckFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthCheck</filter-name>
<url-pattern>/test/api/*</url-pattern>
</filter-mapping>`
public class AuthCheckFilter extends OncePerRequestFilter
{
@Autowired
private AuthCheckService authCheckService;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
log.info("authCheckService"+authCheckService);
Userobject{
username
userage
...
userrolesList // is a List<userrole>
}
userrole{
rolid
rolname
rolcommission
}
可以有一个或多个userrolesList
。
我需要将此列表发送到另一个系统,但另一个系统只接受一个对象列表,如:
userroleobjects
在这种情况下,如果一个用户有10个角色,我需要将10个不同的对象发送到另一个系统。有什么建议我可以轻松地做到这一点而不影响性能吗?
答案 0 :(得分:1)
使用LINQ查询语法非常简单:
var results = from user in userObjects
from role in user.UserRolesList
select new OtherSystemUserObject
{
UserAge = user.UserAge,
UserName = user.UserName,
UserRole = role
// other mapping stuff...
};
答案 1 :(得分:0)
我的朋友,你要做的不是很合乎逻辑。但是,我想到的是:您可以使用您需要发送的类模型创建一个类,然后通过userrolelist
将每个元素转换为单个userrole
对象进行迭代。像这样:
for(int i = 0; i < userobject.userroleslist.Count ; i++)
{
var userrole = userobject.userroleslist[i];
// Map your object with this userrole variable
}