Solved

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

Posted on 2014-10-30
10
110 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
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…

729 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