SolvedPrivate

LINQ: Need to do a Join on ManufacturerId

Posted on 2014-12-11
6
33 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
[X]
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
  • 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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 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