Solved

how to grab a list of id's from a var

Posted on 2014-10-30
10
112 Views
Last Modified: 2014-10-31
I'm trying to grab all the values from one var and populate another var where the first vars Id's exist.

I just can't figure out the syntax. I want to populate ok with all the od.id's that exist. I just don't know how to write it.

 
             
   var o = dc.vw_AdminDate.OrderByDescending(a => a.Date);
                foreach (var i in o)
                {
                    var od = dc.vw_AdminDetails.Where(a => a.Date.Equals(i.Date)).ToList();
                    var ok = dc.Comments.Where(a => a.Id.Equals(od.Id)).ToList();
                       

                    allOrder.Add(new VM_Request("p") { order = i, orderDetails = od, orderComments = ok });
                    
                }

Open in new window

0
Comment
Question by:NickMalloy
[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
  • 6
  • 4
10 Comments
 
LVL 34

Expert Comment

by:it_saige
ID: 40414316
First var is nothing more than a keyword used to imply the type declaration of a given object.

//This line (implicit type declaration)
var i = new List<string>();
// Is the same as this line (explicit type declaration)
List<string> i = new List<string>();

Open in new window


So then the question is actually what are you implying that o and i are.

o looks like it would be a collection of vw_AdminDate, since you are using OrderByDescending(), which returns an Enumerable, this would be an Enumerable<vw_AdminDate>.  So i, then, represents a single vw_AdminDate object.

If this is the case and you want to populate a second enumerable with all elements from o as i that exist in od, you would simply use this:
var ok = (from adminDate in dc.vw_AdminDate
	  from adminDetails in dc.vw_AdminDetails
	  where adminDate.ID.Equals(adminDetails.ID)
	  select adminDetails).OrderByDescending(a => a.Date)

Open in new window


This is all assuming though, that vw_AdminDate and vw_AdminDetails are Enumerable collections themselves.

-saige-
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40414338
Looking at it more you might actually be looking for this:
var ok = (from adminDate in dc.vw_AdminDate
	  from adminDetails in dc.vw_AdminDetails
	  from comment in dc.Comments
	  where comment.ID.Equals(adminDate.ID) && adminDetails.Date.Equals(adminDate.Date)
	  select new 
	  {
	  	Order = adminDate,
		OderDetails = adminDetails,
		Comments = comment
	  })

Open in new window


This is also assuming a one-to-one relationship between the adminDate, adminDetails and comments.

-saige-
0
 

Author Comment

by:NickMalloy
ID: 40414361
I'm actually trying to create a 3 level nested Web Grid. The first level is populated by the var i. It is simply a list of dates.
The send level is individual records from that date. They are in the table vw_AdminDetails. I use the loop to populate. I want to add a third level to this. There is a table called Comments. It contains comments for records stored in the details view. So that is my third nested web grid. So I need the date, then the records, and any comments that pertain to that record. That's where the orderComments = ok in the allOrder.Add(new VM_Request("p") { order = i, orderDetails = od, orderComments = ok });

           
    var o = dc.vw_AdminDate.OrderByDescending(a => a.Date);
                foreach (var i in o)
                {
                    var od = dc.vw_AdminDetails.Where(a => a.Date.Equals(i.Date)).ToList();
                    allOrder.Add(new VM_Request("p") { order = i, orderDetails = od});
                }

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 34

Expert Comment

by:it_saige
ID: 40414369
So If I am understanding you, the first level is a list of dates.  For each date, you can have multiple details.  Does each detail have a single comment or multiple comments?  Can each comment be applid to multiple details or just a single detail?

-saige-
0
 

Author Comment

by:NickMalloy
ID: 40414377
each detail could have none or multiple comments.
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40414388
I don't know if you saw the follow-up question, but can each comment be applied to multiple details or just a single detail?

-saige-
0
 

Author Comment

by:NickMalloy
ID: 40414394
Each comment is attached to one detail. But there can be multiple comments to one detail or none.
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40414540
Ok, here is a quick example I threw together.  Let me know if this is the output you are generally looking for:
using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
	public static void Main()
	{
		List<Order> Orders = new List<Order>()
		{
			new Order() { ID = 1, Date = Convert.ToDateTime("01/01/2014") },
			new Order() { ID = 2, Date = Convert.ToDateTime("02/01/2014") },
			new Order() { ID = 3, Date = Convert.ToDateTime("03/01/2014") },
			new Order() { ID = 4, Date = Convert.ToDateTime("04/01/2014") },
			new Order() { ID = 5, Date = Convert.ToDateTime("05/01/2014") }
		};

		List<Detail> Details = new List<Detail>()
		{
			new Detail() { ID = 1, Date = Convert.ToDateTime("01/01/2014"), Description = "Order number 1 for 01/01/2014" },
			new Detail() { ID = 2, Date = Convert.ToDateTime("01/01/2014"), Description = "Order number 2 for 01/01/2014" },
			new Detail() { ID = 3, Date = Convert.ToDateTime("01/01/2014"), Description = "Order number 3 for 01/01/2014" },
			new Detail() { ID = 4, Date = Convert.ToDateTime("02/01/2014"), Description = "Order number 1 for 02/01/2014" },
			new Detail() { ID = 5, Date = Convert.ToDateTime("02/01/2014"), Description = "Order number 2 for 02/01/2014" },
			new Detail() { ID = 6, Date = Convert.ToDateTime("02/01/2014"), Description = "Order number 3 for 02/01/2014" },
			new Detail() { ID = 7, Date = Convert.ToDateTime("03/01/2014"), Description = "Order number 1 for 03/01/2014" },
			new Detail() { ID = 8, Date = Convert.ToDateTime("03/01/2014"), Description = "Order number 2 for 03/01/2014" },
			new Detail() { ID = 9, Date = Convert.ToDateTime("04/01/2014"), Description = "Order number 1 for 04/01/2014" },
			new Detail() { ID = 10, Date = Convert.ToDateTime("04/01/2014"), Description = "Order number 2 for 04/01/2014" },
			new Detail() { ID = 11, Date = Convert.ToDateTime("04/01/2014"), Description = "Order number 3 for 04/01/2014" },
			new Detail() { ID = 12, Date = Convert.ToDateTime("04/01/2014"), Description = "Order number 4 for 04/01/2014" },
			new Detail() { ID = 13, Date = Convert.ToDateTime("05/01/2014"), Description = "Order number 1 for 05/01/2014" }
		};

		List<Comment> Comments = new List<Comment>()
		{
			new Comment() { ID = 1, DetailID = 1, Description = "Comment 1" },
			new Comment() { ID = 2, DetailID = 1, Description = "Comment 2" },
			new Comment() { ID = 3, DetailID = 1, Description = "Comment 3" },
			new Comment() { ID = 4, DetailID = 3, Description = "Comment 1" },
			new Comment() { ID = 5, DetailID = 3, Description = "Comment 2" },
			new Comment() { ID = 6, DetailID = 4, Description = "Comment 1" },
			new Comment() { ID = 7, DetailID = 5, Description = "Comment 1" },
			new Comment() { ID = 8, DetailID = 6, Description = "Comment 1" },
			new Comment() { ID = 9, DetailID = 7, Description = "Comment 1" },
			new Comment() { ID = 10, DetailID = 13, Description = "Comment 1" },
			new Comment() { ID = 11, DetailID = 13, Description = "Comment 2" },
			new Comment() { ID = 12, DetailID = 13, Description = "Comment 3" },
			new Comment() { ID = 13, DetailID = 14, Description = "Comment 4" }
		};

		var matches = (from order in Orders
					from detail in Details.Where(x => x.Date.Equals(order.Date)).DefaultIfEmpty()
					join comment in Comments on detail.ID equals comment.DetailID into grp
					select new
					{
						OrderDate = order.Date,
						Details = detail.Description,
						Comments = grp
					});
		foreach(var match in matches)
		{
			Console.WriteLine(string.Format("Order Date: {0}; Details - {1}", match.OrderDate.ToShortDateString(), match.Details));
			if (match.Comments.Count() == 0)
				Console.WriteLine(string.Format("No comments for - {0}", match.Details));
			else
			{
				foreach (var comment in match.Comments)
					Console.WriteLine(comment != null ? string.Format("Comment for {0} - {1}", match.Details, comment.Description));
			}
			Console.WriteLine();
		}
		Console.ReadLine();
	}
}

public class Order
{
	public int ID { get; set; }
	public DateTime Date { get; set; }
}

public class Detail
{
	public int ID { get; set; }
	public DateTime Date { get; set; }
	public string Description { get; set; }
}

public class Comment
{
	public int ID { get; set; }
	public int DetailID { get; set; }
	public string Description { get; set; }
}

Open in new window


Produces the following output:Capture.JPG
-saige-
0
 

Author Comment

by:NickMalloy
ID: 40414573
That's the way I need it. I just don't know how to get it to work with 3 nested WebGrids in MVC.
0
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 40414664
Well the real magic for the above example happens here:
var matches = (from order in Orders
		from detail in Details.Where(x => x.Date.Equals(order.Date)).DefaultIfEmpty()
		join comment in Comments on detail.ID equals comment.DetailID into grp
		select new
		{
			OrderDate = order.Date,
			Details = detail.Description,
			Comments = grp
		});

Open in new window


So modifying it for your use would be:
var matches = (from invoice in dc.vw_AdminDate
		from detail in dc.vw_AdminDetails.Where(x => x.Date.Equals(invoice.Date)).DefaultIfEmpty()
		join comment in dc.Comments on detail.Id equals comment.Id into comments
		select new
		{
			Order = invoice,
			Detail = detail,
			Comments = comments
		});

foreach (var match in matches)
	allOrder.Add(new VM_Request("p") { order = match.Order, orderDetails = match.Detail, orderComments = match.Comments });

Open in new window


-saige-
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

626 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