Answered by:
LINQ Query help

Question
-
User-1825561198 posted
Hello :
I have a list
This Linq statement does not work. But that's the Idea
DBDocuments.Where(x => DBDocumentAdditional .Find(a => a.DocumentId == x.DocumentId)).ToList().ForEach(t => t.Additional = DBDocumentAdditional );
Model:
public class Document { public long DocumentId { get; set; } public List<DocumentAdditional> Additional { get; set; } }
public class DocumentAdditional { public long DocumentId { get; set; }
public string Name { get; set; }
public string Value { get; set; } }List<Document> DBDocuments = new List<Document>();
List<DocumentAdditional> DBDocumentAdditional = new List<DocumentAdditional>();
I populate DBDocuments list with some data from database.This List has a key which is DocumentId.
I populate DBDocumentAdditional list with some data from database.This List has a key which is DocumentId.
Now I want to use LINQ to add the DBDocumentAdditional list to the DBDocuments list, for the Matching DocumentId's. List within a List with Matching Id
I am kind of stuck on syntax to do this. Please suggest, or help.
Thank you,
Monday, January 11, 2016 3:55 PM
Answers
-
User281315223 posted
This is because DocuementId is a long and you are attempting to compare it to a string (e.g. "123"). You would likely want to convert or parse this value into a long and then use that as your parameter :
// Convert your value a 64-bit integer (long) var id = Convert.ToInt64("123"); DBDocuments.Where(x => x.DocumentId == id ).ToList().ForEach(t => t.AdditionalAttributes = DBAdditionalAttribute);
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 11, 2016 7:27 PM -
User-2057865890 posted
Hi urpalshu,
I have achieved your needs on my side. You could solve your problem with the suggestion what I gave in the below.
//Get all the ID set List<long> idlist = DBDocuments.Select(c => c.DocumentId).ToList(); //Get the value by ID var query = DBDocumentAdditional.Where(c => idlist.Contains(c.DocumentId)).GroupBy(c => c.DocumentId).Select(c => new { ID = c.Key, list = c.ToList() }); foreach( var item in query) { //assign List<DocumentAdditional> li = item.list; Document dtm = DBDocuments.Where(c => c.DocumentId == item.ID).FirstOrDefault(); dtm.Additional = li; //displaying Response.Write(item.ID); Response.Write("<br />"); foreach(var additem in li) { Response.Write(additem.Name); Response.Write("<br />"); Response.Write(additem.Value); Response.Write("<br />"); } }
Best Regards,Chris Zhao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 12, 2016 8:15 AM
All replies
-
User281315223 posted
This is because DocuementId is a long and you are attempting to compare it to a string (e.g. "123"). You would likely want to convert or parse this value into a long and then use that as your parameter :
// Convert your value a 64-bit integer (long) var id = Convert.ToInt64("123"); DBDocuments.Where(x => x.DocumentId == id ).ToList().ForEach(t => t.AdditionalAttributes = DBAdditionalAttribute);
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 11, 2016 7:27 PM -
User-1825561198 posted
Thank you for the Reply. Idea is Don't want to Hard code the id.
id is saved in DBDocuments and DBDocumentAdditional.
It's kind of like this
DBDocuments.Where(x => DBDocumentAdditional .Find(a => a.DocumentId == x.DocumentId)).ToList().ForEach(t => t.Additional = DBDocumentAdditional );
Monday, January 11, 2016 9:35 PM -
User-2057865890 posted
Hi urpalshu,
I have achieved your needs on my side. You could solve your problem with the suggestion what I gave in the below.
//Get all the ID set List<long> idlist = DBDocuments.Select(c => c.DocumentId).ToList(); //Get the value by ID var query = DBDocumentAdditional.Where(c => idlist.Contains(c.DocumentId)).GroupBy(c => c.DocumentId).Select(c => new { ID = c.Key, list = c.ToList() }); foreach( var item in query) { //assign List<DocumentAdditional> li = item.list; Document dtm = DBDocuments.Where(c => c.DocumentId == item.ID).FirstOrDefault(); dtm.Additional = li; //displaying Response.Write(item.ID); Response.Write("<br />"); foreach(var additem in li) { Response.Write(additem.Name); Response.Write("<br />"); Response.Write(additem.Value); Response.Write("<br />"); } }
Best Regards,Chris Zhao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 12, 2016 8:15 AM -
User281315223 posted
You could perform the conversion within your lambda statements if I understand you correctly :
DBDocuments.Where(x => DBDocumentAdditional.Find(a => a.DocumentId == Convert.ToInt64(x.DocumentId))) .ToList() .ForEach(t => t.Additional = DBDocumentAdditional );
You may have throw an AsEnumerable() or a ToList() call prior to your Find method to ensure that LINQ knows how to resolve that.
Tuesday, January 12, 2016 1:52 PM