Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class like
C#
public SampleClass
{
    public int SampleID
    public int Sample1
    public int Sample2
    .
    .
    .
    public int Samplen

    public string SampleString1
    public string SampleString2
    .
    .
    .
    public string SampleStringn


}

I use that query for use less memorry and server doesn't have difficulty
C#
List<sampleclass> sampleList=(from q in context.SampleTable select new SampleClass{SampleID=q.Sample1,Sample1=q.Sample2}).ToList();

I wonder that if use that is it same as
C#
List<sampleclass> sampleList=(from q in context.SampleTable select new SampleClass{SampleID=q.Sample1,Sample1=q.Sample2,...,Samplen=q.Samplen,SampleString1=q.SampleString1,...,SampleStringn}).ToList();
Posted
Updated 15-Sep-15 20:14pm
v3

If you have properties like Property1...Property<n> and your webmethods return 100,000 rows then you already have lots of architectural problems.

As your properties are ints it might use the same memory weather you populate them or not as 0 takes as much memory to store as 1000. If you want to pass an optional amount of properties then use a parent\child arrangement so each parent has only the properties it needs to pass.
 
Share this answer
 
v2
Don't worry about memory unless you have a genuine problem with it.

Since you are setting more properties then you are allocating more memory in the second case, but again it will make little difference.
 
Share this answer
 
Comments
amagitech 15-Sep-15 10:52am    
We have 150 agency and all agency has 20 user. That's mean 3000 user will query it. And Our table has 100000 rows. And we get datas via WCF. Is it make really difference
Mehdi Gholam 15-Sep-15 10:59am    
Then you should optimize your queries for less data transfer with "where" clauses and paging.
Matt T Heffron 15-Sep-15 16:45pm    
The SampleClass as presented by OP will take exactly the same amount of memory for each instance because each field is a Value type (int) so the space is allocated whether or not it is assigned to.
amagitech 16-Sep-15 2:07am    
if half string half int what changes
Matt T Heffron 16-Sep-15 12:51pm    
Every int field will take exactly 4 bytes, whether or not you assign to it.
Every string field will take the size of a reference (address: 4 or 8 bytes for 32 or 64 bit respectively) plus the size of the string assigned, if any.
These are the allocation values for the CLIENT side.
On the SERVER side, I believe that the query that is actually constructed by LINQ and then executed on the server will (attempt to) select only the columns that are actually referenced in the LINQ statement. So the simpler LINQ statement will use less memory on the server.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900