如何传递linq列表以在mvc中查看 [英] how to pass linq list to view in mvc

查看:83
本文介绍了如何传递linq列表以在mvc中查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面给出了代码

the code is given below

var IS = (from c in xfeed.ciqCompanies
      join s in xfeed.ciqSecurities on c.companyId equals s.companyId
      join ti in xfeed.ciqTradingItems on s.securityId equals ti.securityId
      join e in xfeed.ciqExchanges on ti.exchangeId equals e.exchangeId
      join fp in xfeed.ciqLatestInstanceFinPeriods on c.companyId equals fp.companyId
      join pt in xfeed.ciqPeriodTypes on fp.periodTypeId equals pt.periodTypeId
      join fd in xfeed.ciqFinancialDatas on fp.financialPeriodId equals fd.financialPeriodId
      join di in xfeed.ciqDataItems on fd.dataItemId equals di.dataItemId
      where fd.dataItemId == 3069
      && fp.periodTypeId ==1
      && fp.fiscalYear>=DateTime.Now.AddYears(-5).Year && fp.fiscalYear<=DateTime.Now.Year
      && ti.tickerSymbol == tickerSymbol
      && e.exchangeSymbol == exchangeSymbol
      select new
      {
          c.companyName,
          c.companyId,
          ti.tickerSymbol,
          e.exchangeSymbol,
          pt.periodTypeName,
          fp.fiscalYear,
          fd.dataItemId,
          di.dataItemName,
          fd.dataItemValue
      }).ToList();

推荐答案

不要将任何匿名类型传递给视图。而是创建一个 ViewModel 你的模型&查询它。



阅读本文例如:

http://stackoverflow.com/questions/20319324/how-to-pass-custom-linq-query-to-view [ ^ ]

http://stackoverflow.com/questions/13037282/ pass-linq-query-to-view [ ^ ]



-KR
Don't pass any anonymous type to the view. Rather create one ViewModel of your models & make a query for it.

Read this for example:
http://stackoverflow.com/questions/20319324/how-to-pass-custom-linq-query-to-view[^]
http://stackoverflow.com/questions/13037282/pass-linq-query-to-view[^]

-KR


创建具有属性的具体类(MyViewModel或其他)对于您的字段,如CompanyName,CompanyID,TickerSymbol等,并创建一个List< myviewmodel>从您的查询而不是您现在正在创建的匿名类型。



Create a concrete class (MyViewModel or whatever) that has properties for your fields, so CompanyName, CompanyID, TickerSymbol etc, and create a List<myviewmodel> from your query rather than the anonymous type you're creating now.

select new MyViewModel
      {
          CompanyName = c.companyName,
          CompanyID = c.companyId,
          ...
      }).ToList();





让您的视图接受List< MyViewModel>因为它的类型。不确定上面的代码是否是您需要的确切语法,google将linq查询转换为具体类,如果不是。



Have your view accept List<MyViewModel> as it's type. Not sure if the above code is the exact syntax you need, google for converting linq query to concrete class if it isn't.


研究此链接



https:/ /www.microsoftvirtualacademy.com/en-us/training-courses/introduction-to-asp-net-mvc-8322?l=nKZwZ8Zy_3504984382 [ ^ ]



关于MVC的一切
study this link

https://www.microsoftvirtualacademy.com/en-us/training-courses/introduction-to-asp-net-mvc-8322?l=nKZwZ8Zy_3504984382[^]

Everything about MVC


这篇关于如何传递linq列表以在mvc中查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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