Solved

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

Posted on 2014-10-30
10
102 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 32

Expert Comment

by:it_saige
Comment Utility
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 32

Expert Comment

by:it_saige
Comment Utility
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
Comment Utility
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
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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
Comment Utility
each detail could have none or multiple comments.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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
Comment Utility
Each comment is attached to one detail. But there can be multiple comments to one detail or none.
0
 
LVL 32

Expert Comment

by:it_saige
Comment Utility
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
Comment Utility
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 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now