@@ -26,38 +26,35 @@ IAccountService accountService
2626 /// </summary>
2727 public IEnumerable < MarketplaceListingViewModel > GetAllListings ( )
2828 {
29- var listings = context . PostedItem
29+ var items = context . PostedItem
3030 . Include ( x => x . Category )
3131 . Include ( x => x . Condition )
3232 . Include ( x => x . Status )
3333 . Include ( x => x . PostImage )
3434 . Where ( item => item . StatusId != 3 )
3535 . OrderByDescending ( item => item . PostedAt )
36- . Join (
37- context . ACCOUNT ,
38- item => item . PostedById . ToString ( ) ,
39- acc => acc . gordon_id ,
40- ( item , acc ) => new { item , acc }
41- )
42- . Select ( x => new MarketplaceListingViewModel
43- {
44- Id = x . item . Id ,
45- PostedAt = x . item . PostedAt ,
46- Name = x . item . Name ,
47- Price = x . item . Price ,
48- CategoryId = x . item . CategoryId ,
49- CategoryName = x . item . Category . CategoryName ,
50- Detail = x . item . Detail ,
51- ConditionId = x . item . ConditionId ,
52- ConditionName = x . item . Condition . ConditionName ,
53- StatusId = x . item . StatusId ,
54- StatusName = x . item . Status . StatusName ,
55- ImagePaths = x . item . PostImage . Select ( img => img . ImagePath ) . ToList ( ) ,
56- PosterUsername = x . acc != null ? x . acc . AD_Username : null
57- } )
58- . ToList ( ) ;
36+ . ToList ( ) ; // Materialize to memory
37+
38+ var accountDict = context . ACCOUNT
39+ . Where ( a => items . Select ( i => i . PostedById . ToString ( ) ) . Distinct ( ) . Contains ( a . gordon_id ) )
40+ . ToDictionary ( a => a . gordon_id , a => a . AD_Username ) ;
5941
60- return listings ;
42+ return items . Select ( item => new MarketplaceListingViewModel
43+ {
44+ Id = item . Id ,
45+ PostedAt = item . PostedAt ,
46+ Name = item . Name ,
47+ Price = item . Price ,
48+ CategoryId = item . CategoryId ,
49+ CategoryName = item . Category ? . CategoryName ,
50+ Detail = item . Detail ,
51+ ConditionId = item . ConditionId ,
52+ ConditionName = item . Condition ? . ConditionName ,
53+ StatusId = item . StatusId ,
54+ StatusName = item . Status ? . StatusName ,
55+ ImagePaths = item . PostImage ? . Select ( img => img . ImagePath ) . ToList ( ) ?? new List < string > ( ) ,
56+ PosterUsername = accountDict . TryGetValue ( item . PostedById . ToString ( ) , out var username ) ? username : null
57+ } ) . ToList ( ) ;
6158 }
6259
6360 public IEnumerable < MarketplaceListingViewModel > GetUserListings ( string username )
@@ -67,38 +64,35 @@ public IEnumerable<MarketplaceListingViewModel> GetUserListings(string username)
6764
6865 int userId = int . Parse ( account . GordonID ) ;
6966
70- var listings = context . PostedItem
67+ var items = context . PostedItem
7168 . Include ( x => x . Category )
7269 . Include ( x => x . Condition )
7370 . Include ( x => x . Status )
7471 . Include ( x => x . PostImage )
7572 . Where ( item => item . PostedById == userId && item . StatusId != 3 )
7673 . OrderByDescending ( item => item . PostedAt )
77- . Join (
78- context . ACCOUNT ,
79- item => item . PostedById . ToString ( ) ,
80- acc => acc . gordon_id ,
81- ( item , acc ) => new { item , acc }
82- )
83- . Select ( x => new MarketplaceListingViewModel
84- {
85- Id = x . item . Id ,
86- PostedAt = x . item . PostedAt ,
87- Name = x . item . Name ,
88- Price = x . item . Price ,
89- CategoryId = x . item . CategoryId ,
90- CategoryName = x . item . Category . CategoryName ,
91- Detail = x . item . Detail ,
92- ConditionId = x . item . ConditionId ,
93- ConditionName = x . item . Condition . ConditionName ,
94- StatusId = x . item . StatusId ,
95- StatusName = x . item . Status . StatusName ,
96- ImagePaths = x . item . PostImage . Select ( img => img . ImagePath ) . ToList ( ) ,
97- PosterUsername = x . acc != null ? x . acc . AD_Username : null
98- } )
9974 . ToList ( ) ;
10075
101- return listings ;
76+ var accountDict = context . ACCOUNT
77+ . Where ( a => items . Select ( i => i . PostedById . ToString ( ) ) . Distinct ( ) . Contains ( a . gordon_id ) )
78+ . ToDictionary ( a => a . gordon_id , a => a . AD_Username ) ;
79+
80+ return items . Select ( item => new MarketplaceListingViewModel
81+ {
82+ Id = item . Id ,
83+ PostedAt = item . PostedAt ,
84+ Name = item . Name ,
85+ Price = item . Price ,
86+ CategoryId = item . CategoryId ,
87+ CategoryName = item . Category ? . CategoryName ,
88+ Detail = item . Detail ,
89+ ConditionId = item . ConditionId ,
90+ ConditionName = item . Condition ? . ConditionName ,
91+ StatusId = item . StatusId ,
92+ StatusName = item . Status ? . StatusName ,
93+ ImagePaths = item . PostImage ? . Select ( img => img . ImagePath ) . ToList ( ) ?? new List < string > ( ) ,
94+ PosterUsername = accountDict . TryGetValue ( item . PostedById . ToString ( ) , out var username ) ? username : null
95+ } ) . ToList ( ) ;
10296 }
10397
10498 /// <summary>
@@ -344,32 +338,28 @@ public IEnumerable<MarketplaceListingViewModel> GetFilteredListings(
344338 // Pagination
345339 query = query . Skip ( ( page - 1 ) * pageSize ) . Take ( pageSize ) ;
346340
347- var listings = query
348- . Join (
349- context . ACCOUNT ,
350- item => item . PostedById . ToString ( ) ,
351- acc => acc . gordon_id ,
352- ( item , acc ) => new { item , acc }
353- )
354- . Select ( x => new MarketplaceListingViewModel
355- {
356- Id = x . item . Id ,
357- PostedAt = x . item . PostedAt ,
358- Name = x . item . Name ,
359- Price = x . item . Price ,
360- CategoryId = x . item . CategoryId ,
361- CategoryName = x . item . Category . CategoryName ,
362- Detail = x . item . Detail ,
363- ConditionId = x . item . ConditionId ,
364- ConditionName = x . item . Condition . ConditionName ,
365- StatusId = x . item . StatusId ,
366- StatusName = x . item . Status . StatusName ,
367- ImagePaths = x . item . PostImage . Select ( img => img . ImagePath ) . ToList ( ) ,
368- PosterUsername = x . acc != null ? x . acc . AD_Username : null
369- } )
370- . ToList ( ) ;
341+ var items = query . ToList ( ) ;
371342
372- return listings ;
343+ var accountDict = context . ACCOUNT
344+ . Where ( a => items . Select ( i => i . PostedById . ToString ( ) ) . Distinct ( ) . Contains ( a . gordon_id ) )
345+ . ToDictionary ( a => a . gordon_id , a => a . AD_Username ) ;
346+
347+ return items . Select ( item => new MarketplaceListingViewModel
348+ {
349+ Id = item . Id ,
350+ PostedAt = item . PostedAt ,
351+ Name = item . Name ,
352+ Price = item . Price ,
353+ CategoryId = item . CategoryId ,
354+ CategoryName = item . Category ? . CategoryName ,
355+ Detail = item . Detail ,
356+ ConditionId = item . ConditionId ,
357+ ConditionName = item . Condition ? . ConditionName ,
358+ StatusId = item . StatusId ,
359+ StatusName = item . Status ? . StatusName ,
360+ ImagePaths = item . PostImage ? . Select ( img => img . ImagePath ) . ToList ( ) ?? new List < string > ( ) ,
361+ PosterUsername = accountDict . TryGetValue ( item . PostedById . ToString ( ) , out var username ) ? username : null
362+ } ) . ToList ( ) ;
373363 }
374364
375365
@@ -434,37 +424,38 @@ public IEnumerable<MarketplaceAdminViewModel> GetAdminThreads(
434424 query = query . Where ( x => x . Name . Contains ( search ) || x . Detail . Contains ( search ) ) ;
435425
436426 // Group by thread (OriginalPostId or Id if null), select latest version per thread
437- var threads = query
438- . AsEnumerable ( )
427+ var threadList = query
428+ . ToList ( )
439429 . GroupBy ( x => x . OriginalPostId ?? x . Id )
440- . Select ( g => g . OrderByDescending ( x => x . Id ) . First ( ) ) ;
430+ . Select ( g => g . OrderByDescending ( x => x . Id ) . First ( ) )
431+ . ToList ( ) ;
441432
442433 // Sorting
443434 switch ( sortBy ? . ToLower ( ) )
444435 {
445436 case "price" :
446- threads = desc ? threads . OrderByDescending ( x => x . Price ) : threads . OrderBy ( x => x . Price ) ;
437+ threadList = desc ? threadList . OrderByDescending ( x => x . Price ) . ToList ( ) : threadList . OrderBy ( x => x . Price ) . ToList ( ) ;
447438 break ;
448439 case "date" :
449- threads = desc ? threads . OrderByDescending ( x => x . PostedAt ) : threads . OrderBy ( x => x . PostedAt ) ;
440+ threadList = desc ? threadList . OrderByDescending ( x => x . PostedAt ) . ToList ( ) : threadList . OrderBy ( x => x . PostedAt ) . ToList ( ) ;
450441 break ;
451442 case "title" :
452- threads = desc ? threads . OrderByDescending ( x => x . Name ) : threads . OrderBy ( x => x . Name ) ;
443+ threadList = desc ? threadList . OrderByDescending ( x => x . Name ) . ToList ( ) : threadList . OrderBy ( x => x . Name ) . ToList ( ) ;
453444 break ;
454445 default :
455- threads = threads . OrderByDescending ( x => x . PostedAt ) ; // Default: newest first
446+ threadList = threadList . OrderByDescending ( x => x . PostedAt ) . ToList ( ) ; // Default: newest first
456447 break ;
457448 }
458449
459450 // Pagination
460- threads = threads . Skip ( ( page - 1 ) * pageSize ) . Take ( pageSize ) ;
451+ threadList = threadList . Skip ( ( page - 1 ) * pageSize ) . Take ( pageSize ) . ToList ( ) ;
461452
462- var userIds = threads . Select ( x => x . PostedById . ToString ( ) ) . Distinct ( ) . ToList ( ) ;
453+ var userIds = threadList . Select ( x => x . PostedById . ToString ( ) ) . Distinct ( ) . ToList ( ) ;
463454 var accountDict = context . ACCOUNT
464455 . Where ( a => userIds . Contains ( a . gordon_id ) )
465456 . ToDictionary ( a => a . gordon_id , a => a . AD_Username ) ;
466457
467- return threads . Select ( item => new MarketplaceAdminViewModel
458+ return threadList . Select ( item => new MarketplaceAdminViewModel
468459 {
469460 Id = item . Id ,
470461 PostedAt = item . PostedAt ,
@@ -488,38 +479,35 @@ public IEnumerable<MarketplaceAdminViewModel> GetAdminThreads(
488479 /// </summary>
489480 public IEnumerable < MarketplaceListingViewModel > GetThreadEditHistory ( int threadId )
490481 {
491- var history = context . PostedItem
482+ var items = context . PostedItem
492483 . Include ( x => x . Category )
493484 . Include ( x => x . Condition )
494485 . Include ( x => x . Status )
495486 . Include ( x => x . PostImage )
496487 . Where ( x => x . OriginalPostId == threadId || x . Id == threadId )
497488 . OrderByDescending ( item => item . Id )
498- . Join (
499- context . ACCOUNT ,
500- item => item . PostedById . ToString ( ) ,
501- acc => acc . gordon_id ,
502- ( item , acc ) => new { item , acc }
503- )
504- . Select ( x => new MarketplaceListingViewModel
505- {
506- Id = x . item . Id ,
507- PostedAt = x . item . PostedAt ,
508- Name = x . item . Name ,
509- Price = x . item . Price ,
510- CategoryId = x . item . CategoryId ,
511- CategoryName = x . item . Category . CategoryName ,
512- Detail = x . item . Detail ,
513- ConditionId = x . item . ConditionId ,
514- ConditionName = x . item . Condition . ConditionName ,
515- StatusId = x . item . StatusId ,
516- StatusName = x . item . Status . StatusName ,
517- ImagePaths = x . item . PostImage . Select ( img => img . ImagePath ) . ToList ( ) ,
518- PosterUsername = x . acc != null ? x . acc . AD_Username : null
519- } )
520489 . ToList ( ) ;
521490
522- return history ;
491+ var accountDict = context . ACCOUNT
492+ . Where ( a => items . Select ( i => i . PostedById . ToString ( ) ) . Distinct ( ) . Contains ( a . gordon_id ) )
493+ . ToDictionary ( a => a . gordon_id , a => a . AD_Username ) ;
494+
495+ return items . Select ( item => new MarketplaceListingViewModel
496+ {
497+ Id = item . Id ,
498+ PostedAt = item . PostedAt ,
499+ Name = item . Name ,
500+ Price = item . Price ,
501+ CategoryId = item . CategoryId ,
502+ CategoryName = item . Category ? . CategoryName ,
503+ Detail = item . Detail ,
504+ ConditionId = item . ConditionId ,
505+ ConditionName = item . Condition ? . ConditionName ,
506+ StatusId = item . StatusId ,
507+ StatusName = item . Status ? . StatusName ,
508+ ImagePaths = item . PostImage ? . Select ( img => img . ImagePath ) . ToList ( ) ?? new List < string > ( ) ,
509+ PosterUsername = accountDict . TryGetValue ( item . PostedById . ToString ( ) , out var username ) ? username : null
510+ } ) . ToList ( ) ;
523511 }
524512
525513
@@ -645,4 +633,4 @@ public int GetAdminThreadsCount(
645633 return count ;
646634 }
647635 }
648- }
636+ }
0 commit comments