Solved

W/ LINQ return 1 product per location

Posted on 2013-11-12
4
45 Views
Last Modified: 2016-03-15
I have a map with products at locations.

Right now, I'm returning many products for each location.

However, I want to return a single product for each location.

This is my LINQ code.



		if (!String.IsNullOrEmpty(lystId))
		{
			//Only products in a lyst
			lystDistinct = (from l in dc.Lysts
								join mp in dc.MemberProductLysts on l.LystId equals mp.LystId
								join p in dc.Products on mp.ProductId equals p.ProductId
								join t in dc.Tags on p.TagId equals t.TagId
								join a in dc.Addresses on t.AddressId equals a.AddressId
								where l.LystId == new Guid(lystId)
								select a.AddressId).Distinct().ToList();
		}
		else
		{
			//All of the members products
			lystDistinct = (from l in dc.Lysts
								join mp in dc.MemberProductLysts on l.LystId equals mp.LystId
								join p in dc.Products on mp.ProductId equals p.ProductId
								join t in dc.Tags on p.TagId equals t.TagId
								join a in dc.Addresses on t.AddressId equals a.AddressId
								where l.MemberId == new Guid(System.Web.Security.Membership.GetUser().ProviderUserKey.ToString())
								select a.AddressId).Distinct().ToList();
		}

		var locationList = (from p in dc.Products
							join t in dc.Tags on p.TagId equals t.TagId
							join a in dc.Addresses on t.AddressId equals a.AddressId
							join pi in dc.ProductImages on p.ProductId equals pi.ProductId
							join i in dc.Images on pi.ImageId equals i.ImageId
							where lystDistinct.Contains(a.AddressId)
							where p.IsActive == true
							where t.IsActive == true
							// where t.IsPaying == true
							where t.RegionId == member.RegionId
							orderby dc.Random()
							select new { a.Latitude, a.Longitude, brandName = t.Name, productName = p.Name, i.FileName }).Distinct();

Open in new window

0
Comment
Question by:Rob Gaudet
  • 2
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39644565
It sounds like you need the First operator, but I can't see where to add that.  You would need to find the correct sequence to apply that to.
0
 

Author Comment

by:Rob Gaudet
ID: 39648023
I was thinking the same thing, such as in a subquery of some kind.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39648084
Is location based on a record in Address?

       join a in dc.Addresses on t.AddressId equals a.AddressId

I would think that you might be able to use a group by expression, with a FirstOrDefault operator:

var sessions =  
    from m in this.members                     
    join s in 
    (
        from se in this.sessions
        group se by se.Name into g
        select new {Name = g.Key, SessionEndTime = g.FirstOrDefault()}
    )   
    on m.Name equals s.Name                    
    select new { MemberName = m.Name, SessionTime = s.SessionEndTime}

Open in new window

0
 

Author Comment

by:Rob Gaudet
ID: 39648102
That makes sense. I will give it a try.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

948 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

22 Experts available now in Live!

Get 1:1 Help Now