[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# LINQ Question

Posted on 2011-05-02
Medium Priority
285 Views
Experts,

Lets say I`m using a LING query on a List of objects of type ObjABC. How do I update the values of the List? Lets say I get some results out of the query. I need to update the results so that it reflects in the original List.

Casting ObjVar to the object type doesn`t seem to work.

In simple terms, I need to update the original ObjList with values from ObjVar (I do some calculation here...For the sake of soimplicity I`m multiplying the values by 120 and 190). Hope that makes sense.

Thanks,
San
``````var ObjVar = from Info in ObjList
group Info by Info.Time into Grp
select new
{
Time = Grp.Key,
A = Grp.Max(Ent => (Ent.Alpha == Enum.A) ? Ent.Delta : (double?)null) * 120,
B = Grp.Max(Ent => (Ent.Alpha == Enum.B) ? Ent.Delta : (double?)null) * 190,
};

ObjList = (ObjectABC)ObjVar.ToList();  //Error Here
``````
0
Question by:San24
• 4

LVL 75

Expert Comment

ID: 35508152
You selected "new" which creates a new anonymous type. What is the definition of ObjectABC? Perhaps a proper conversion can be worked out.
0

LVL 75

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 35508163
Also, you are trying to cast  a List<something> to a type ObjectABC. This cast is most likely illegal unless you've overridden the "explicit" operator.
0

Author Comment

ID: 35508257
@Kaufmed - The cast is illegal. No doubt about it.

I was wondering if I could change the underlying List using  LINQ. The code below shows manipulation without using LINQ.

ObjectABC - Time, Alpha and Delta. This is just for simplicity, the actual object is more complicated.
Based on the Alpha values, I`m trying to change the Delta.

``````for (int j = 0; j < ObjList.Count; j++)
{
if (ObjList[i].Alpha == Enum.A)
{
ObjList[i].Delta *= 120;
}

if (ObjList[i].Alpha == Enum.B)
{
ObjList[i].Delta *= 190;
}
}
``````
0

LVL 75

Expert Comment

ID: 35508513
I was wondering if I could change the underlying List using  LINQ. The code below shows manipulation without using LINQ.
Well you can, but not when you "select new", due to the aforementioned anonymous type creation.

I'm not sure I fully understand what you are doing in your original Linq post, but I'm going to throw this out there to see if I understand the problem  = )
``````ObjList.Where(item => item.Alpha == Enum.A).ToList().ForEach(x => x.Delta *= 120);
ObjList.Where(item => item.Alpha == Enum.B).ToList().ForEach(x => x.Delta *= 190);
``````
0

LVL 75

Assisted Solution

käµfm³d   👽 earned 2000 total points
ID: 35508577
Not the prettiest, but with one loop:
``````ObjList.Where(item => item.Alpha == Enum.A || item.Alpha == Enum.B).ToList().ForEach(x => x.Delta *= x.Alpha == Enum.A ? 120 : 190);
``````
0

## Featured Post

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
###### Suggested Courses
Course of the Month20 days, 3 hours left to enroll