Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
SolvedPrivate

LINQ: Need to do a Join on ManufacturerId

Posted on 2014-12-11
6
29 Views
Last Modified: 2016-02-16
I have DealerManufacturers and each Manufacturer has a Bool property. I need the Count of DealerManufacturer which have that property as true.

public class Manufacturer
{
  public virtual id { get; set; }
  public virtual IsServiceable { get { return id != 45; } }
}

public class DealerManufacturer
{
        public virtual int DealerId  { get; set; }
        public virtual int ManufacturerId  { get; set; }
 }

I have numerous an IList<DealerManufacturers> in the model and need a count of those which are IsServiceable is TRUE

model.DealerManufacturers

Thanks.
0
Comment
Question by:newbieweb
  • 3
  • 3
6 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40494423
Hi newbieweb;

Something like this should give you the count.

IList<DealerManufacturers> dmList = ....;
IList<Manufacturer> mList = ....;

var serviceable = (from dm in dmList
                   from m in mList
                   where dm.DealerId == m.id && m.IsServiceable == true
                   select dm).Count();

Open in new window

0
 

Author Comment

by:newbieweb
ID: 40494431
Sorry, I forgot to say. I only use the Fluent LINQ API...
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40494491
var serviceableCount = dmList.Join(mList, 
                       dm => dm.DealerId, 
                       m => m.id,
                       (dm, m) => m.m.IsServiceable == true).Count();

Open in new window

0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:newbieweb
ID: 40494606
I ended up using a different syntax and two queries. Is this inferior to yours? I could not get yours to work:

            IList<Manufacturer> newSaleManufacturers = DefaultRepository.FindByQuery(new FindManufacturersQuery()).Where(x => x.IsServiceable && x.IsNewSale).ToList();

            bool sellsNew = newSaleManufacturers.Where(x => model.DealerManufacturers.Select(y => y.ManufacturerId).Contains(x.Id)).Count() > 0;
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40496349
Can you post how you implemented the solution I posted please.
0
 

Author Closing Comment

by:newbieweb
ID: 40507179
Sorry, no time. Your post helped.

Thanks.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question