LINQ查询+将结果传递给MVC中的视图 [英] LINQ query + pass result to the view in MVC

查看:229
本文介绍了LINQ查询+将结果传递给MVC中的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个记录在数据库表中的CD集合。

每个人都被分配了一个GenreID。例如。 1 =古典,2 =流行,3 =民间等。

每张CD都为每个流派的序列分配一个数字。这是手动完成的。

当我录制一张新CD时,我希望在我看来将我选择的类型中的下一个数字分配给输入表单。

我欢迎Linq查询的帮助,并将结果输入到视图中的输入框。

亲切的问候

Jim



我尝试了什么:



I have a collection of CDs recorded in a db table.
Each one is assigned a GenreID. eg. 1= Classical, 2 = Pop, 3= Folk etc.
Each CD is assigned a number from a sequence for each Genre. This is done manually at the moment.
When I record a new CD,I would like to have the next number in my chosen Genre allocated to an input form in my view.
I would welcome help with the Linq query and with getting the result to the input box within the view.
Kind regards
Jim

What I have tried:

My view currently sends the GenreId to the Controller.
The method has the following Linq:-
<pre>var query =( from DM in c.DiscMain
                        where DM.GenreId==GenreId       
                        orderby DM.DiscNo descending
                        select DM).Take(1);
                        return Json(query);

推荐答案

获取给定类型的Max()id。这是为该类型指定的最后一个。

Get the Max() id for a given genre. That's the last one assigned for that genre.
... Where( x => x.genre == z ).Max( x => x.id ).SingleOrDefault();



将其增加1以使下一个分配。


Increment it by 1 to get the next one to assign.


这篇关于LINQ查询+将结果传递给MVC中的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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