Solved

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

Posted on 2014-10-30
10
107 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 33

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 33

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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 33

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 33

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 33

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 33

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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