简体   繁体   中英

display it into the “Table1” table

Here are the methods mentioned above:

public IList<tst> testUsers()
{
    IList<tst> testUsers = _test.GetAll().ToList();
    return test(test);
}

To display Users with Location I think you need one class called AdsnapshotUsers

public class AdsnapshotUsers
{
// three fields UserId, UserLogonName, Location

}

Now create one method which return IList<AdsnapshotUsers>

 public IList<AdsnapshotUsers> GetAdsnapshotUsers()
{
List<User> Users =  GetAcitveUsers().ToList();
List<ADSnapshot> adSnapshotUsers = _adSnapshotRepository.GetAll().ToList();

     return (from u in Users  
         join ad in adSnapshotUsers on u.UserLogonName equals ad.UserLogonName 
         select new AdsnapshotUsers { 
             UserId= u.UserId, UserLogonName = u.UserLogonName, Location = ad.Location
         }
     ).ToList<AdsnapshotUsers>();
}

Left Outer Join to display all the values from the user table eventhough if a userlogonname is not present in the adsnapshot table (location value blank)

 (from u in Users 
  join ad in adSnapshotUsers on u.UserLogonName equals ad.UserLogonName into aduserselect
  from ad1 in aduserselect.DefaultIfEmpty() 
  select new AdsnapshotUsers { 
      UserId= u.UserId, UserLogonName = u.UserLogonName, Location = ad1.Location
  }
 ).ToList<AdsnapshotUsers>();

Here all records from user table will get selected and for location if userlogonname exist then location name value set with ADSnapshot table value otherwise not exist then default empty value set.

The simplest way to join two IEnumerables in LINQ is with Join():

   var joined = from u in GetActiveUsers()
                join ad in _adSnapshotRepository.GetAll()
                    on u.UserLogonName equals ad.UserLogonName
                select new { User = u, Location = ad.Location }

Of course, joined is now an IEnumerable of an anonymous type. If you are displaying this information in a grid view or something that requires a flat object, you may want to create your own class with the properties you want from each table and select that instead.

Two other things to note:

(1) you always seem to call ToList() early to convert from IQueryable to IEnumerable. By putting off ToList() until you've done all your joining and filtering, you can get most of your query to happen in SQL (rather than in memory).

(2) rather than joining, it is often preferable to set up associtation properties on your entities and use those:

 IQueryable<User> users = ...
 var combinedInfo = users.Select(u => new { User = u, Location = u.ADSnapshot.Location })
    .ToList();

If you have relationship (1:1) between these two tables then you can get records very easily using just one query. and evenif records are not present in second table.

I am using above defined class definition to store data.

_userRepository.GetAll().Select(u=>new AdsnapshotUsers  {
         UserId= u.UserId, 
         UserLogonName = u.UserLogonName, 
         Location = u.ADSnapshot.Location
 }).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM