Using LinQ to access multiple tables. What is the correct way to join these tables with LinQ to be the same as SQL Query?

huangapple go评论97阅读模式
英文:

Using LinQ to access multiple tables. What is the correct way to join these tables with LinQ to be the same as SQL Query?

问题

I am trying to access all the tables in the data warehouse that I created for an experimental project using Lin-Q and I just don't seem to understand how to access every table regarding the data structure, but can easily query using SQL Server to get my results. I can access the DimLocation table, but none of the others. I think in the controller is the right approach, but don't know how to select each column of every table using Lin-Q. Any help would be greatly appreciated!

My Program: [GlobalCommandCenter][1]

SQL Query Result Join Tables:

[![enter image description here][2]][2]

Tables To Join From Entities Framework:
[![enter image description here][3]][3]

LinQ I Created to Join Tables (Need help to join all tables as SQL):
[![enter image description here][4]][4]

I can use one table and it works just fine, but I get this error when I try to do multiple tables as Michael describes to do.
[![enter image description here][5]][5]

Added multiple joins code in the controller:

  1. LocationDatas = (List<DimLocation>)(from c in entities.DimLocations
  2. join f in entities.FactInventories
  3. on c.LocationId equals f.LocationId
  4. join p in entities.DimProductInventories
  5. on f.ProductId equals p.ProductId
  6. join dd in entities.DimDates
  7. on f.DateSK equals dd.DateSK
  8. join dt in entities.DimTimes
  9. on f.TimeSK equals dt.TimeSK
  10. where c.ContinentName == continent && c.CountryName == country
  11. && c.CountryName == c.CountryName && c.ContinentName == c.ContinentName
  12. && c.ContinentName == continent || string.IsNullOrEmpty(continent) || string.IsNullOrEmpty(country)
  13. && c.ContinentName == continent
  14. select new
  15. {
  16. c.LocationId,
  17. c.ContinentName,
  18. c.CountryName,
  19. c.RegionName,
  20. c.CityName,
  21. p.ProductName,
  22. f.ProductPrice,
  23. f.ProductQty,
  24. dd.StandardDate,
  25. dt.StandardTime
  26. }),

Error I get after adding multiple joins code:
[![enter image description here][6]][6]

I edited the call to the model, did away with the bracket and added a semicolon at the very end of it. I then added var to each output with a semi colon at the very end of each statement. After that I get errors on the view.
[![Error Controller Problem][7]][7]
Compilation Error on View (IEnumerable Compatible Error in Razor View?):
[![Compilation Error on View][8]][8]

SQL Query:

  1. SELECT ContinentName, CountryName, RegionName, CityName, ProductName,
  2. finv.ProductPrice,
  3. finv.ProductQty, StandardDate, StandardTime
  4. FROM DimLocation dloc
  5. INNER JOIN FactInventory finv ON finv.LocationId = dloc.LocationId
  6. INNER JOIN DimProductInventory dprod ON finv.ProductId = dprod.ProductId
  7. INNER JOIN DimDate ddat ON finv.DateSK = ddat.DateSK
  8. INNER JOIN DimTime dtim ON finv.TimeSK = dtim.TimeSK
  9. SELECT * FROM FactInventory
  10. SELECT * FROM DimLocation
  11. SELECT * FROM DimDate
  12. SELECT * FROM DimTime
  13. SELECT * FROM DimProductInventory

Controller:

  1. using ClassLibraryDAL.DAL;
  2. using OperationsCommandCenter.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. namespace OperationsCommandCenter.Controllers
  9. {
  10. public class HomeController : Controller
  11. {
  12. // GET: Home
  13. public ActionResult Index()
  14. {
  15. CommandCenterModel model = PopulateModel(null, null);
  16. return View(model);
  17. }
  18. //Controls DropDownList.
  19. [HttpPost]
  20. public ActionResult Index(string continent, string country)
  21. {
  22. CommandCenterModel model = PopulateModel(continent, country);
  23. return View(model);
  24. }
  25. //Population Controller. References list selection.
  26. public static CommandCenterModel PopulateModel(string continent, string country)
  27. {
  28. using (GlobalCommandCenter3Entities entities = new GlobalCommandCenter3Entities())
  29. {
  30. CommandCenterModel model = new CommandCenterModel()
  31. {
  32. //Join all the tables together and extract the information into one web grid.
  33. LocationDatas = (from c in entities.DimLocations
  34. join f in entities.FactInventories
  35. on c.LocationId equals f.LocationId
  36. join p in entities.DimProductInventories
  37. on f.ProductId equals p.ProductId
  38. join dd in entities.DimDates
  39. on f.DateSK equals dd.DateSK
  40. join dt in entities.DimTimes
  41. on f.TimeSK equals dt.TimeSK
  42. where c.ContinentName == continent && c.CountryName == country && c.CountryName == c.CountryName
  43. && c.ContinentName == c.ContinentName && c.ContinentName == continent || string.IsNullOrEmpty(continent) ||
  44. string.IsNullOrEmpty(country) && c.ContinentName == continent
  45. select c).ToList(),
  46. Country = (from c in entities.DimLocations
  47. orderby c.CountryName
  48. where !string.IsNullOrEmpty(c.CountryName) && c.CountryName != null && continent == c.ContinentName
  49. select new SelectListItem { Text = c.CountryName, Value = c.CountryName }).Distinct().ToList(),
  50. Continent = (from c in entities.DimLocations
  51. orderby c.ContinentName
  52. where (c.ContinentName == c.ContinentName)
  53. select new SelectListItem { Text = c.ContinentName, Value = c.ContinentName }).Distinct().ToList(),
  54. /*
  55. Location = (from c in entities.DimLocations
  56. join f in entities.FactInventories
  57. on c.LocationId equals f.LocationId
  58. join p in entities.DimProductInventories
  59. on f.ProductId equals p.ProductId
  60. join dd in entities.DimDates
  61. on f.DateSK equals dd.DateSK
  62. join dt in entities.DimTimes
  63. on f.TimeSK equals dt.TimeSK
  64. where c.ContinentName == continent && c.CountryName == country && c.CountryName == c.CountryName
  65. && c.ContinentName == c.ContinentName && c.ContinentName == continent || string.IsNullOrEmpty(continent) ||
  66. string.IsNullOrEmpty(country) && c.ContinentName == continent
  67. select new { c.ContinentName, c.CountryName, c.RegionName, c.CityName, f.ProductPrice,
  68. f.ProductQty, dd.StandardDate, dt.StandardTime }).ToList(),
  69. */
  70. };
  71. //Allow to stay on selection.
  72. model.SectionCity = continent;
  73. return model;
  74. }
  75. }
  76. }
  77. }

Model:

  1. using ClassLibraryDAL.DAL;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. namespace OperationsCommandCenter.Models
  8. {
  9. public class CommandCenterModel
  10. {
  11. public List<DimLocation> LocationDatas { get; set; }
  12. public List<DimProductInventory> ProductInventory { get; set; }
  13. public List<SelectListItem> Country { get; set; }
  14. public List<SelectListItem> Continent { get; set; }
  15. public List<SelectListItem> ProductName { get;
  16. <details>
  17. <summary>英文:</summary>
  18. I am trying to access all the tables in the data warehouse that I created for an experimental project using Lin-Q and I just don&#39;t seem to understand how to access every table regarding the data structure, but can easily query using SQL Server to get my results. I can access the DimLocation table, but none of the others. I think in the controller is the right approach, but don&#39;t know how to select each column of every table using Lin-Q. Any help would be greatly appreciated!
  19. My Program: [GlobalCommandCenter][1]
  20. SQL Query Result Join Tables:
  21. [![enter image description here][2]][2]
  22. Tables To Join From Entities Framework:
  23. [![enter image description here][3]][3]
  24. LinQ I Created to Join Tables (Need help to join all tables as SQL):
  25. [![enter image description here][4]][4]
  26. I can use one table and it works just fine, but I get this error when I try to do multiple tables as Michael describes to do.
  27. [![enter image description here][5]][5]
  28. Added multiple joins code in the controller:
  29. LocationDatas = (List&lt;DimLocation&gt;)(from c in entities.DimLocations
  30. join f in entities.FactInventories
  31. on c.LocationId equals f.LocationId
  32. join p in entities.DimProductInventories
  33. on f.ProductId equals p.ProductId
  34. join dd in entities.DimDates
  35. on f.DateSK equals dd.DateSK
  36. join dt in entities.DimTimes
  37. on f.TimeSK equals dt.TimeSK
  38. where c.ContinentName == continent &amp;&amp; c.CountryName == country
  39. &amp;&amp; c.CountryName == c.CountryName &amp;&amp; c.ContinentName == c.ContinentName
  40. &amp;&amp; c.ContinentName == continent || string.IsNullOrEmpty(continent) || string.IsNullOrEmpty(country)
  41. &amp;&amp; c.ContinentName == continent
  42. select new
  43. {
  44. c.LocationId,
  45. c.ContinentName,
  46. c.CountryName,
  47. c.RegionName,
  48. c.CityName,
  49. p.ProductName,
  50. f.ProductPrice,
  51. f.ProductQty,
  52. dd.StandardDate,
  53. dt.StandardTime
  54. }),
  55. Error I get after adding multiple joins code:
  56. [![enter image description here][6]][6]
  57. I edited the call to the model, did away with the bracket and added a semicolon at the very end of it. I then added var to each output with a semi colon at the very end of each statement. After that I get errors on the view.
  58. [![Error Contoller Problem][7]][7]
  59. Compilation Error on View (IEnumerable Compatible Error in Razor View?):
  60. [![Compilation Error on View][8]][8]
  61. public static CommandCenterModel PopulateModel(string continent, string country)
  62. {
  63. using (GlobalCommandCenter3Entities entities = new GlobalCommandCenter3Entities())
  64. {
  65. **CommandCenterModel model = new CommandCenterModel()
  66. {**
  67. //Join all the tables together and extract the information into one web grid.
  68. LocationDatas = (from c in entities.DimLocations
  69. join f in entities.FactInventories
  70. on c.LocationId equals f.LocationId
  71. join p in entities.DimProductInventories
  72. on f.ProductId equals p.ProductId
  73. join dd in entities.DimDates
  74. on f.DateSK equals dd.DateSK
  75. join dt in entities.DimTimes
  76. on f.TimeSK equals dt.TimeSK
  77. where c.ContinentName == continent &amp;&amp; c.CountryName == country &amp;&amp; c.CountryName == c.CountryName
  78. &amp;&amp; c.ContinentName == c.ContinentName &amp;&amp; c.ContinentName == continent || string.IsNullOrEmpty(continent)
  79. || string.IsNullOrEmpty(country) &amp;&amp; c.ContinentName == continent
  80. select c).ToList(),
  81. Country = (from c in entities.DimLocations
  82. orderby c.CountryName
  83. where !string.IsNullOrEmpty(c.CountryName) &amp;&amp; c.CountryName != null &amp;&amp; continent == c.ContinentName
  84. select new SelectListItem { Text = c.CountryName, Value = c.CountryName }).Distinct().ToList(),
  85. Continent = (from c in entities.DimLocations
  86. orderby c.ContinentName
  87. where (c.ContinentName == c.ContinentName)
  88. select new SelectListItem { Text = c.ContinentName, Value = c.ContinentName }).Distinct().ToList(),
  89. /*
  90. Location = (from c in entities.DimLocations
  91. join f in entities.FactInventories
  92. on c.LocationId equals f.LocationId
  93. join p in entities.DimProductInventories
  94. on f.ProductId equals p.ProductId
  95. join dd in entities.DimDates
  96. on f.DateSK equals dd.DateSK
  97. join dt in entities.DimTimes
  98. on f.TimeSK equals dt.TimeSK
  99. where c.ContinentName == continent &amp;&amp; c.CountryName == country &amp;&amp; c.CountryName == c.CountryName &amp;&amp; c.ContinentName == c.ContinentName &amp;&amp; c.ContinentName == continent || string.IsNullOrEmpty(continent) || string.IsNullOrEmpty(country) &amp;&amp; c.ContinentName == continent
  100. select new { c.ContinentName, c.CountryName, c.RegionName, c.CityName, f.ProductPrice, f.ProductQty, dd.StandardDate, dt.StandardTime }).ToList(),
  101. */
  102. };
  103. //Allow to stay on selection.
  104. model.SectionCity = continent;
  105. return model;
  106. }
  107. }
  108. SQL Query:
  109. SELECT ContinentName, CountryName, RegionName, CityName, ProductName,
  110. finv.ProductPrice,
  111. finv.ProductQty, StandardDate, StandardTime
  112. FROM DimLocation dloc
  113. INNER JOIN FactInventory finv ON finv.LocationId = dloc.LocationId
  114. INNER JOIN DimProductInventory dprod ON finv.ProductId = dprod.ProductId
  115. INNER JOIN DimDate ddat ON finv.DateSK = ddat.DateSK
  116. INNER JOIN DimTime dtim ON finv.TimeSK = dtim.TimeSK
  117. SELECT * FROM FactInventory
  118. SELECT * FROM DimLocation
  119. SELECT * FROM DimDate
  120. SELECT * FROM DimTime
  121. SELECT * FROM DimProductInventory
  122. Controller:
  123. using ClassLibraryDAL.DAL;
  124. using OperationsCommandCenter.Models;
  125. using System;
  126. using System.Collections.Generic;
  127. using System.Linq;
  128. using System.Web;
  129. using System.Web.Mvc;
  130. namespace OperationsCommandCenter.Controllers
  131. {
  132. public class HomeController : Controller
  133. {
  134. // GET: Home
  135. public ActionResult Index()
  136. {
  137. CommandCenterModel model = PopulateModel(null, null);
  138. return View(model);
  139. }
  140. //Controls DropDownList.
  141. [HttpPost]
  142. public ActionResult Index(string continent, string country)
  143. {
  144. CommandCenterModel model = PopulateModel(continent, country);
  145. return View(model);
  146. }
  147. //Population Controller. References list selection.
  148. public static CommandCenterModel PopulateModel(string continent, string country)
  149. {
  150. using (GlobalCommandCenter3Entities entities = new
  151. GlobalCommandCenter3Entities())
  152. {
  153. CommandCenterModel model = new CommandCenterModel()
  154. {
  155. //Join all the tables together and extract the information into one web
  156. grid.
  157. LocationDatas = (from c in entities.DimLocations
  158. join f in entities.FactInventories
  159. on c.LocationId equals f.LocationId
  160. join p in entities.DimProductInventories
  161. on f.ProductId equals p.ProductId
  162. join dd in entities.DimDates
  163. on f.DateSK equals dd.DateSK
  164. join dt in entities.DimTimes
  165. on f.TimeSK equals dt.TimeSK
  166. where c.ContinentName == continent &amp;&amp; c.CountryName ==
  167. country &amp;&amp; c.CountryName == c.CountryName &amp;&amp; c.ContinentName == c.ContinentName
  168. &amp;&amp; c.ContinentName == continent || string.IsNullOrEmpty(continent) ||
  169. string.IsNullOrEmpty(country) &amp;&amp; c.ContinentName == continent
  170. select c).ToList(),
  171. Country = (from c in entities.DimLocations
  172. orderby c.CountryName
  173. where !string.IsNullOrEmpty(c.CountryName) &amp;&amp; c.CountryName
  174. != null &amp;&amp; continent == c.ContinentName
  175. select new SelectListItem { Text = c.CountryName, Value =
  176. c.CountryName }).Distinct().ToList(),
  177. Continent = (from c in entities.DimLocations
  178. orderby c.ContinentName
  179. where (c.ContinentName == c.ContinentName)
  180. select new SelectListItem { Text = c.ContinentName,
  181. Value = c.ContinentName }).Distinct().ToList(),
  182. /*
  183. Location = (from c in entities.DimLocations
  184. join f in entities.FactInventories
  185. on c.LocationId equals f.LocationId
  186. join p in entities.DimProductInventories
  187. on f.ProductId equals p.ProductId
  188. join dd in entities.DimDates
  189. on f.DateSK equals dd.DateSK
  190. join dt in entities.DimTimes
  191. on f.TimeSK equals dt.TimeSK
  192. where c.ContinentName == continent &amp;&amp; c.CountryName ==
  193. country &amp;&amp; c.CountryName == c.CountryName &amp;&amp; c.ContinentName == c.ContinentName
  194. &amp;&amp; c.ContinentName == continent || string.IsNullOrEmpty(continent) ||
  195. string.IsNullOrEmpty(country) &amp;&amp; c.ContinentName == continent
  196. select new { c.ContinentName, c.CountryName,
  197. c.RegionName, c.CityName, f.ProductPrice, f.ProductQty, dd.StandardDate,
  198. dt.StandardTime }).ToList(),
  199. */
  200. };
  201. //Allow to stay on selection.
  202. model.SectionCity = continent;
  203. return model;
  204. }
  205. }
  206. }
  207. }
  208. Model:
  209. using ClassLibraryDAL.DAL;
  210. using System;
  211. using System.Collections.Generic;
  212. using System.Linq;
  213. using System.Web;
  214. using System.Web.Mvc;
  215. namespace OperationsCommandCenter.Models
  216. {
  217. public class CommandCenterModel
  218. {
  219. public List&lt;DimLocation&gt; LocationDatas { get; set; }
  220. public List&lt;DimProductInventory&gt; ProductInventory { get; set; }
  221. public List&lt;SelectListItem&gt; Country { get; set; }
  222. public List&lt;SelectListItem&gt; Continent { get; set; }
  223. public List&lt;SelectListItem&gt; ProductName { get; set; }
  224. //public List&lt;DimLocation&gt; Location { get; set; }
  225. public string SectionCity { get; set; }
  226. }
  227. }
  228. View:
  229. @model OperationsCommandCenter.Models.CommandCenterModel
  230. @{
  231. ViewBag.Title = &quot;Home Page&quot;;
  232. string Country = Model.Country.ToString();
  233. string SectionCity = Model.SectionCity;
  234. WebGrid webGrid = new WebGrid(source: Model.LocationDatas, canPage: true, canSort:
  235. true, sortDirectionFieldName: &quot;ContinentName, CountryName, RegionName, CityName&quot;,
  236. rowsPerPage: 20);
  237. webGrid.Pager(WebGridPagerModes.All);
  238. }
  239. &lt;!DOCTYPE html&gt;
  240. &lt;head&gt;
  241. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width&quot; /&gt;
  242. &lt;title&gt;Global Command Center&lt;/title&gt;
  243. &lt;link href=&quot;@Url.Content(&quot;~/Content/YardDogStyle.css&quot;)&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;
  244. /&gt;
  245. &lt;link href=&quot;@Url.Content(&quot;~/Scripts/jquery-3.4.1.min.js&quot;)&quot; rel=&quot;stylesheet&quot;
  246. type=&quot;text/css&quot; /&gt;
  247. &lt;link href=&quot;@Url.Content(&quot;~/Scripts/bootstrap.min.js&quot;)&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;
  248. /&gt;
  249. &lt;script src=&quot;~/Scripts/bootstrap.min.js&quot;&gt;&lt;/script&gt;
  250. &lt;script src=&quot;~/Scripts/jquery-3.4.1.min.js&quot;&gt;&lt;/script&gt;
  251. &lt;script src=&quot;Scripts/jquery-1.4.1.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
  252. &lt;script src=&quot;~/Scripts/jquery-1.4.1.min.js&quot;&gt;&lt;/script&gt;
  253. &lt;link href=&quot;~/Content/YardDogStyle.css&quot; rel=&quot;stylesheet&quot; /&gt;
  254. &lt;/head&gt;
  255. &lt;body&gt;
  256. &lt;div id=&quot;time&quot;&gt;&lt;/div&gt;
  257. &lt;div id=&quot;RowCount&quot;&gt;&lt;/div&gt;
  258. &lt;SCRIPT LANGUAGE=&quot;Javascript&quot;&gt;
  259. function checkTime(i) {
  260. if (i &lt; 10) {
  261. i = &quot;0&quot; + i;
  262. }
  263. return i;
  264. }
  265. function startTime() {
  266. var today = new Date();
  267. var h = today.getHours();
  268. var m = today.getMinutes();
  269. var s = today.getSeconds();
  270. // add a zero in front of numbers&lt;10
  271. m = checkTime(m);
  272. s = checkTime(s);
  273. document.getElementById(&#39;time&#39;).innerHTML = h + &quot;:&quot; + m + &quot;:&quot; + s; //Get the
  274. time.
  275. document.getElementById(&#39;time&#39;).innerHTML = &quot;Date: &quot; + today; //Get the Date.
  276. t = setTimeout(function () {
  277. startTime()
  278. }, 500);
  279. }
  280. startTime();
  281. &lt;/SCRIPT&gt;
  282. &lt;script src=&quot;Scripts/jquery-1.4.1.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
  283. &lt;script src=&quot;Scripts/ScrollableGridPlugin.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
  284. &lt;script type=&quot;text/javascript&quot;&gt;
  285. $(document).ready(function () {
  286. $(&#39;#&lt;%=webGrid.ClientID %&gt;&#39;).Scrollable({
  287. ScrollHeight: 300
  288. });
  289. $(&#39;#&lt;%=webGrid.ClientID %&gt;&#39;).Scrollable({
  290. ScrollHeight: 300
  291. });
  292. });
  293. &lt;/script&gt;
  294. &lt;script src=&quot;Scripts/ScrollableGridPlugin.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
  295. &lt;script src=&quot;~/Scripts/ScrollableGridPlugin.js&quot;&gt;&lt;/script&gt;
  296. &lt;script type=&quot;text/javascript&quot;&gt;
  297. $(document).ready(function () {
  298. $(&#39;#&lt;%=webGrid.ClientID %&gt;&#39;).Scrollable({
  299. ScrollHeight: 300,
  300. Width: 467
  301. });
  302. });
  303. &lt;/script&gt;
  304. &lt;script type=&quot;text/javascript&quot;&gt;
  305. //Count the rows on the webGrid that are visible... is my intent with this code.
  306. function CountRows() {
  307. var totalRowCount = 0;
  308. var rowCount = 0;
  309. var gridView = document.getElementById(&quot;&lt;%=webGrid.ClientID %&gt;&quot;);
  310. var rows = gridView.getElementsByTagName(&quot;tr&quot;)
  311. for (var i = 0; i &lt; rows.length; i++) {
  312. totalRowCount++;
  313. if (rows[i].getElementsByTagName(&quot;td&quot;).length &gt; 0) {
  314. rowCount++;
  315. }
  316. }
  317. var message = &quot;Total Row Count: &quot; + totalRowCount;
  318. message += &quot;\nRow Count: &quot; + rowCount;
  319. //alert(message);
  320. message = document.getElementById(&#39;RowCount&#39;).innerHTML = message;
  321. // return false;
  322. }
  323. CountRows();
  324. &lt;/script&gt;
  325. &lt;form id=&quot;formYardDog&quot; class=&quot;formYardDog&quot; runat=&quot;server&quot; method=&quot;post&quot;&gt;
  326. @{ int firstRecord = (webGrid.PageIndex * webGrid.RowsPerPage) + 1;
  327. int lastRecord = (webGrid.PageIndex * webGrid.RowsPerPage) + webGrid.Rows.Count;
  328. // webGrid.Rows..GetRowCount(DataGridViewElementStates.Visible);
  329. }
  330. &lt;div id=&quot;RowCountTop&quot;&gt;&lt;b&gt;Records: @firstRecord - @lastRecord of
  331. @webGrid.TotalRowCount&lt;/b&gt;&lt;/div&gt;&lt;br /&gt;
  332. @using (Html.BeginForm(&quot;Index&quot;, &quot;Home&quot;, FormMethod.Post, new { @Id = &quot;formYardDog&quot;
  333. }))
  334. {
  335. //Loop to Get Dictionary List Buttons with Distinct Section Values.
  336. for (int i = 0; i &lt; Model.Continent.Count; i++)
  337. {
  338. //If the SectionCity is not null, then back-ground color to red.
  339. if (@Model.Continent[i].Value == SectionCity)
  340. {
  341. &lt;button name=&quot;@Html.NameFor(model =&gt; model.Continent)&quot;
  342. value=&quot;@Model.Continent[i].Value&quot; id=&quot;ddlSectionButtons&quot; runat=&quot;server&quot; , new {
  343. class=&quot;ddlSectionButtons&quot; onClick=&quot;focusMe(this);&quot; style=&quot;background-color:
  344. #AFE1AF; color: black&quot; }&gt;@Model.Continent[i].Value&lt;/button&gt;
  345. }
  346. else
  347. {
  348. // var Count = i;
  349. &lt;!-- &lt;input type=&quot;submit&quot; name=&quot;Html.NameFor(model =&gt; model.Section)&quot;
  350. value=Model.Section[i].Value id=&quot;ddlSectionButtons&quot; , new {
  351. class=&quot;ddlSectionButtons&quot; onClick=&quot;focusMe(this);&quot; }/&gt; --&gt;
  352. &lt;button name=&quot;@Html.NameFor(model =&gt; model.Continent)&quot;
  353. value=&quot;@Model.Continent[i].Value&quot; id=&quot;ddlSectionButtons&quot; runat=&quot;server&quot; , new {
  354. class=&quot;ddlSectionButtons&quot; onClick=&quot;focusMe(this);&quot;
  355. }&gt;@Model.Continent[i].Value&lt;/button&gt;
  356. }
  357. }
  358. if (SectionCity == null || SectionCity == &quot;&quot;)
  359. {
  360. &lt;button text=&quot;All&quot; type=&quot;submit&quot; name=&quot;@Html.NameFor(model =&gt;
  361. model.Continent)&quot; , new { onClick=&quot;focusMe(this);&quot; id=&quot;ddlSectionAllButton&quot;
  362. class=&quot;ddlSectionAllButton&quot; placeholder=&quot;All&quot; style=&quot;background-color: #AFE1AF;
  363. color: black&quot; })&gt;All&lt;/button&gt;
  364. }
  365. else
  366. {
  367. &lt;button text=&quot;All&quot; type=&quot;submit&quot; name=&quot;@Html.NameFor(model =&gt;
  368. model.Continent)&quot; , new { onClick=&quot;focusMe(this);&quot; id=&quot;ddlSectionAllButton&quot;
  369. class=&quot;ddlSectionButtons&quot; placeholder=&quot;All&quot; style=&quot;background-color: #045AC6;
  370. color: white&quot; })&gt;All&lt;/button&gt;
  371. }
  372. &lt;br /&gt;
  373. //Section == Country //ContinentLocation == PlantLocation
  374. @Html.DropDownListFor(model =&gt; model.Continent, Model.Continent, &quot;- Continent -
  375. &quot;, new { onchange = &quot;document.forms[0].submit();&quot;, @id = &quot;ddlWarehouses&quot;, @class =
  376. &quot;ddlWarehouses&quot; })
  377. @Html.DropDownListFor(model =&gt; model.Country, Model.Country, &quot; - Country -&quot;, new
  378. { onchange = &quot;document.forms[0].submit();&quot;, @id = &quot;ddlSection&quot;, @class =
  379. &quot;ddlSection&quot; })
  380. &lt;div id=&quot;content&quot;&gt;
  381. @webGrid.GetHtml(tableStyle: &quot;webgrid-table&quot;,
  382. headerStyle: &quot;webgrid-header&quot;,
  383. footerStyle: &quot;webgrid-footer&quot;,
  384. //alternatingRowStyle: &quot;webgrid-alternating-row&quot;,
  385. selectedRowStyle: &quot;webgrid-selected-row&quot;,
  386. rowStyle: &quot;webgrid-row-style&quot;,
  387. mode: WebGridPagerModes.All,
  388. htmlAttributes: new { @id = &quot;webGrid&quot; },
  389. columns: webGrid.Columns(
  390. webGrid.Column(header: &quot;Actions&quot;, format:@&lt;span class=&quot;link&quot;&gt;
  391. &lt;a href=&quot;#&quot; class=&quot;collapse expand-btn&quot;&gt;Expand&lt;/a&gt;
  392. &lt;a href=&quot;#&quot; class=&quot;expand collapse-btn&quot;&gt;Collapse&lt;/a&gt;
  393. &lt;!--
  394. &lt;a class=&quot;Edit&quot; href=&quot;javascript:;&quot;&gt;Edit&lt;/a&gt;
  395. &lt;a class=&quot;Clear&quot; href=&quot;javascript:;&quot;&gt;Clear&lt;/a&gt;
  396. &lt;a class=&quot;Update&quot; href=&quot;javascript:;&quot; style=&quot;display:none&quot;&gt;Update&lt;/a&gt;
  397. &lt;a class=&quot;Cancel&quot; href=&quot;javascript:;&quot; style=&quot;display:none&quot;&gt;Cancel&lt;/a&gt;
  398. --&gt;
  399. &lt;/span&gt;),
  400. webGrid.Column(columnName: &quot;Country&quot;, header: &quot;Country&quot;, format: @&lt;div&gt;
  401. &lt;label id=&quot;ContinentLbl&quot; class=&quot;label&quot;&gt;&lt;a id=&quot;CountryNameLnk&quot;
  402. href=&quot;javascript:;&quot;&gt;@item.ContinentName&lt;/a&gt;&lt;/label&gt;
  403. &lt;!--- &lt;input id=&quot;Location&quot; class=&quot;text&quot; type=&quot;text&quot; value=&quot;item.CountryName&quot;
  404. style=&quot;display:none&quot; onkeyup=&quot;this.value = this.value.toUpperCase();&quot;/&gt; --&gt;
  405. &lt;br /&gt;
  406. &lt;label id=&quot;CountryLbl&quot; &gt;&lt;a id=&quot;RegioNameLnk&quot; href=&quot;#&quot; class=&quot;collapse
  407. expand-btn&quot; &gt;@item.CountryName&lt;/a&gt;&lt;/label&gt;
  408. &lt;br /&gt;
  409. &lt;label id=&quot;RegionLbl&quot; &gt;&lt;a id=&quot;CityNameLnk&quot;href=&quot;#&quot; class=&quot;collapse expand-
  410. btn&quot; &gt;@item.RegionName&lt;/a&gt;&lt;/label&gt;
  411. &lt;br /&gt;
  412. &lt;label id=&quot;CityLbl&quot; class=&quot;expand collapse&quot; &gt;@item.CityName&lt;/label&gt;
  413. &lt;/div&gt;, style: &quot;CountryName&quot;),
  414. webGrid.Column(header: &quot;RowPageID&quot;, format: @&lt;div&gt;
  415. &lt;label id=&quot;LocationIDLbl&quot; class=&quot;label&quot;&gt;@item.LocationID&lt;/label&gt;
  416. &lt;/div&gt;, style: &quot;LocationID&quot;))),
  417. &lt;div id=&quot;RowCountBpttom&quot;&gt;&lt;b&gt;Records: @firstRecord - @lastRecord of
  418. @webGrid.TotalRowCount&lt;/b&gt;&lt;/div&gt;
  419. &lt;/div&gt;
  420. &lt;br /&gt;
  421. &lt;div class=&quot;WebGridTable&quot;&gt;
  422. &lt;/div&gt;
  423. }
  424. &lt;/form&gt;
  425. &lt;script type=&quot;text/javascript&quot;
  426. src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&quot;&gt;&lt;/script&gt;
  427. &lt;script type=&quot;text/javascript&quot;
  428. src=&quot;https://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js&quot;&gt;&lt;/script&gt;
  429. &lt;script src=&quot;~/Scripts/YardDog.js&quot;&gt;&lt;/script&gt;
  430. &lt;script type=&quot;text/javascript&quot;&gt;
  431. //Expand the Nodes.
  432. $(function () {
  433. $(&#39;.expand&#39;).hide(); // Default - hide the table row of Course information
  434. // and also hide the Collapse text from
  435. // Student information&#39;s action column
  436. $(&#39;.expand-btn, .collapse-btn&#39;).on(&quot;click&quot;, function () {
  437. var tr = $(this).parents(&#39;tr:first&#39;);
  438. tr.find(&#39;.expand, .collapse&#39;).toggle(); // toggle to display either
  439. Expand or
  440. // Collapse text in the Student row
  441. tr.next().toggle(); // toggle to display table row with Course
  442. information
  443. });
  444. });
  445. &lt;/script&gt;
  446. &lt;/body&gt;
  447. [1]: https://drive.google.com/file/d/1DP9vY4RdS9LcJJWqDt6G-DtRgKsEnOo2/view
  448. [2]: https://i.stack.imgur.com/hwhSH.jpg
  449. [3]: https://i.stack.imgur.com/A1i94.jpg
  450. [4]: https://i.stack.imgur.com/e4fDA.jpg
  451. [5]: https://i.stack.imgur.com/vlZ4y.jpg
  452. [6]: https://i.stack.imgur.com/mN7p0.jpg
  453. [7]: https://i.stack.imgur.com/v94A5.jpg
  454. [8]: https://i.stack.imgur.com/2Jeto.jpg
  455. </details>
  456. # 答案1
  457. **得分**: 0
  458. 获取自定义投影作为查询结果,您可以选择将其选择为匿名类或创建另一个DTO用于此
  459. 由于您的代码是截图,无法复制,因此以下是修剪过的示例:
  460. ```csharp
  461. var customProjection = from c in entities.DimLocations
  462. join f in entities.FactInventories
  463. on c.LocationId equals f.LocationId
  464. select new {
  465. c.ContinentName,
  466. f.ProductPrice
  467. }

通过这种方式,您可以访问来自不同表的字段。

英文:

To get a custom projection as a result of the query you can either select it to an anonymous class or create another DTO for this.

Trimmed example (due to your code being a screenshot - I can't copy it):

  1. var customProjection = from c in entities.DimLocations
  2. join f in entities.FactInventories
  3. on c.LocationId equals f.LocationId
  4. select new {
  5. c.ContinentName,
  6. f.ProductPrice
  7. }

This way you can access fields from different tables.

huangapple
  • 本文由 发表于 2023年2月14日 05:05:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定