Quick Q for the C# & EF Devs

DA-LION-619

Honorary Master
Joined
Aug 22, 2009
Messages
13,777
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.
 

^^vampire^^

Expert Member
Joined
Feb 17, 2009
Messages
3,877
public class myService
{
public IEnumerable<string> Whatever {get; set;}
}

private void myFunction()
{
var myWhateverList = myService.Whatever.ToList();
}
 

Ancalagon

Honorary Master
Joined
Feb 23, 2010
Messages
18,140
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?
 

mister

Executive Member
Joined
Jul 21, 2008
Messages
9,157
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:

skimread

Honorary Master
Joined
Oct 18, 2010
Messages
12,419
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;}
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
What exactly would be an integral use case for this?
 

gkm

Expert Member
Joined
May 10, 2005
Messages
1,519
[)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.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
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
 

gkm

Expert Member
Joined
May 10, 2005
Messages
1,519
[)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.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
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.
 

benhart

Active Member
Joined
Apr 7, 2010
Messages
48
[)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...
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
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