Solved

W/ LINQ return 1 product per location

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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…

792 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