1

I have Student list with below data

/*----------------------- |Student | ------------------------- | ID | Name | Dept | ------------------------- | 101 | Peter | IT | | 102 | John | IT | | 103 | Ronald | Mech | | 104 | Sam | Comp | -----------------------*/ 

Other list say Extra with below data

/*---------------------- | StudentId | Dept | ------------------------ | 101 | Civil | | 103 | Chemical | ----------------------*/ 

Now I want following result

/*------------------------- |Student | --------------------------- | ID | Name | Dept | --------------------------- | 101 | Peter | Civil | | 102 | John | IT | | 103 | Ronald | Chemical | | 104 | Sam | Comp | -------------------------*/ 

Currently I have written below logic:

foreach(item in Extra) { //Search item in Student list //Update it } 

I need more efficient way (don't want to use iteration) using LINQ.

2 Answers 2

1

Try something like this. Didn't test it though.

var query = from s in student join e in extra on s.ID == e.StudentId select new {s.ID, s.Name, (e.Dept != null) ? e.Dept:s.Dept}; 
Sign up to request clarification or add additional context in comments.

2 Comments

I am using DataTable for Student. So when I write above query it will not update the row state. I need rowstate of the StudentIds 101 and 103 to be Modified.
LINQ = Language Integrated Query: Queries by nature do not modify. You can interate over them, and do the stuff you need to do. If you want to get only the rows where the Depts differ, simply add where e.Dept != s.Dept.
0

LINQ is using iteration internally, too, so there is no performance benefit in using LINQ. Because LINQ has very general implementations, it might even be slower, because it can't make assumptions about your data, which you can make in your custom loop.

1 Comment

Ok, agree with the performance issue. But can you give the LINQ query I just want to check how to use it in this case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.