Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

W/ LINQ return 1 product per location

Posted on 2013-11-12
4
Medium Priority
?
68 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

772 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