如何通过C#列表替换LINQ查询中的硬编码值

时间:2014-08-30 16:37:40

标签: c# sql-server linq

如何用此查询中名为ListOfAuditors的List中的值替换硬编码数组new int [] {102,7,39}?

public IList<UserDetails> GetAllAssociatesForGivenListOfAuditors(IList<UserDetails> ListOfAuditors)
        {
            using (OPMSDataSourceDataContext db = new OPMSDataSourceDataContext())
            {
                var query = (from AuditorAssociatemaps in db.AuditorAssociatemaps
                             where
                               (new int[] { 102, 7, 39 }).Contains(AuditorAssociatemaps.AuditorID)
                             group new { AuditorAssociatemaps.UserData, AuditorAssociatemaps } by new
                             {
                                 AuditorAssociatemaps.UserData.FirstName,
                                 AuditorAssociatemaps.AssociateID
                             } into g
                             orderby
                               g.Key.FirstName
                             select new UserDetails
                             {
                                 FirstName = g.Key.FirstName,
                                 UserID = g.Key.AssociateID
                             }).ToList();
                return query;
            }
        }

1 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情:

...    
var ids = ListOfAuditors.Select(a => a.Id)
...

,在查询中:

where ids.Contains(AuditorAssociatemaps.AuditorID)