Solved

W/ LINQ return 1 product per location

Posted on 2013-11-12
4
55 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp web application 3 55
Securing WEBAPI on Azure 2 55
asp.net repeater 2 35
.NET universe documentation poster 2 22
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 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