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

x
?
Solved

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

Posted on 2014-10-30
10
Medium Priority
?
116 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
  • 6
  • 4
10 Comments
 
LVL 35

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 35

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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 35

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 35

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 35

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 35

Accepted Solution

by:
it_saige earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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 Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

824 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