score:1

Accepted answer

Use the ForEach and Count methods:

List<LeadImportViewModel> leadImportViewModelList = new List<LeadImportViewModel>();
List<LeadGridViewModel> leadGridViewModelList = new List<LeadGridViewModel>();

leadImportViewModelList.ForEach(vm => vm.QtyDuplicates 
    = leadGridViewModelList.Count(
    gv => gv.CompanyNameStripped = vm.CompanyNameStripped));

score:1

It sounds like you just need:

var count = leadImports.Select(x => x.CompanyNameStripped))
                       .Intersect(leadGrids.Select(x => x.CompanyNameStripped)))
                       .Count();

Or:

var names = new HashSet<string>(leadImports.Select(x => x.CompanyNameStripped));
var count = leadGrids.Count(x => names.Contains(x.CompanyNameStripped));

score:1

list.Select(a => a.CompanyNameStripped).Intersect(list2.Select(b => b.CompanyNameStripped).Count()

score:0

Try

var coll = (from t1 in db.Leads_Staging
                select new LeadImportViewModel
                    {
                        Id = t1.Id,
                        CompanyName = t1.CompanyName,
                        CompanyNameStripped = t1.CompanyNameStripped,
                        QtyDuplicates = db.LeadGridViewModel.Count(
                                        gv => gv.CompanyNameStripped == t1.CompanyNameStripped)
                    });

Related Articles