Solved

W/ LINQ return 1 product per location

Posted on 2013-11-12
4
43 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

18 Experts available now in Live!

Get 1:1 Help Now