SolvedPrivate

LINQ: Need to do a Join on ManufacturerId

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

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
Sorry, I forgot to say. I only use the Fluent LINQ API...
0
 
LVL 62

Accepted Solution

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

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:newbieweb
Comment Utility
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 62

Expert Comment

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

Author Closing Comment

by:newbieweb
Comment Utility
Sorry, no time. Your post helped.

Thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now