SolvedPrivate

LINQ: Need to do a Join on ManufacturerId

Posted on 2014-12-11
6
30 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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