Solved

# LINQ Question

Posted on 2011-05-02
284 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

LVL 74

Expert Comment

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 74

Accepted Solution

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

@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 74

Expert Comment

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 74

Assisted Solution

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…