Quick Q for the C# & EF Devs

DA-LION-619

Honorary Master
Joined
Aug 22, 2009
Messages
13,774
Reaction score
1,613
Location
Randburg
So using Entity Framework, you can't have something like

Code:
public List<string> Whatever {get;set;}

Now I've been using a normal string and adding the values separated by a comma, so when I retrieve them I need to do a Split etc...

My question is, which would be faster between my current method and saving the list of values as JSON and serializing/deserializing that.
 
public class myService
{
public IEnumerable<string> Whatever {get; set;}
}

private void myFunction()
{
var myWhateverList = myService.Whatever.ToList();
}
 
Well think about having a list of strings would mean in Entity Framework. There is no list data type in databases. Think about it - how would you create a table that itself stores a list? Not possible in SQL Server.

The only way you can do what you want to do, is by creating a second table that contains your string property and a foreign key to the the table that contains the list property. Then you don't have a list of strings, your first table would contain an IEnumerable of the second table.

Hope that makes sense?
 
My question is, which would be faster between my current method and saving the list of values as JSON and serializing/deserializing that.

Splitting a string would be less work... but honestly with EF overhead who is going to notice any difference between a string split & json serialization? You could get much better speed improvements by ensuring you are using EF in the most efficient way.

Performance Considerations for Entity Framework 4, 5, and 6
 
Last edited:
THis looks like a lookup

so have something like
Code:
class Lookup{
     int Id{get;set:}
     string Name {get;set;}
}
then
Code:
public IEnumerable<Lookup> WhateverSingleItem {get;set;}
 
What exactly would be an integral use case for this?
 
[)roi(];17722812 said:
What exactly would be an integral use case for this?

If you mean the Postgres arrays, then I guess it is to avoid sad tricks like storing the data comma separated in a text field or as json in a text field etc. I just wanted to point out it is possible and I have seen them used.
 
If you mean the Postgres arrays, then I guess it is to avoid sad tricks like storing the data comma separated in a text field or as json in a text field etc. I just wanted to point out it is possible and I have seen them used.
Wasn't bothered by Postgres but rather design; i.e. anti-pattern
 
[)roi(];17725527 said:
Wasn't bothered by Postgres but rather design; i.e. anti-pattern

Yes, I cannot really recommend using it, both in terms of complications in working with it and lack of portability to other RDBMS's. I tend to prefer to use mainstream DB features only, since then one will not have hassles with any tools in using it. And the well trodden path is less likely to hit DB bugs or DB upgrade hassles.
 
Yes, I cannot really recommend using it, both in terms of complications in working with it and lack of portability to other RDBMS's. I tend to prefer to use mainstream DB features only, since then one will not have hassles with any tools in using it. And the well trodden path is less likely to hit DB bugs or DB upgrade hassles.
Agreed, hence my question; like BLOBs: Arrays, and custom delimited storage are just obvious indicators of inexperience.
If you need to store file structured content then put it on the system designed for it.
 
[)roi(];17730515 said:
Agreed, hence my question; like BLOBs: Arrays, and custom delimited storage are just obvious indicators of inexperience.
If you need to store file structured content then put it on the system designed for it.

What? There are many use cases for storing an array on a record. I'm surprised you haven't encountered one in all your years of experience...
 
What? There are many use cases for storing an array on a record. I'm surprised you haven't encountered one in all your years of experience...

As I said before:
[)roi(];17722812 said:
What exactly would be an integral use case for this?

If you believe there's a case where this approach is preferred over convention means then answer my question?
 
Top
Sign up to the MyBroadband newsletter
X