0

Let's say I have an Entity Object 'Jewels' that has the properties 'Name' and 'Birthdate'. I want to implement a LINQ query that returns an object that has 'Name', 'Birthdate' and 'Birthstone'. So I extend 'Jewels' like this:

public partial class JewelStones : Jewels

string Birthstone = null;
public void JewelsWithStone()
{
     this.Birthstone = "diamond";
      //(we figure out what stone applies to the month here)
}

I can get this far, and I THINK I'm on the right track, but I don't know how to write a LINQ query and get back an object that includes Birthstone, so I can bind that object to a grid that will show Birthstone, which I'm not storing anywhere, as it's always calculated (this is pretend data, sorry if it's not logical).

List<Jewel> jewels = new List<Jewel>;
using (jewelentities db = new jewelentities())
{
    jewels = (from j in db.Jewels select j).ToList();
}

How do I fill up my JewelStone object with Name, Birthdate, and Birthstone?

If I'm not following best practice here, please let me know!

EDIT

I've tried adding a partial class to the Entity partial class. When I reference the Jewel class now, it 'sees' the Birthstone property, but it is null. I don't know why? Here is the partial class:

public partial class Jewel
{
    private string _birthstone;
    public string Birthstone
    {
        get { return _birthstone; }
        set
        {
            JewelBusiness jewelBusiness = new JewelBusiness();
            _birthstone = jewelBusiness.RequestBirthstone(birthmonth); 
        }
    }
}

If I use LINQ to query the entity to get a list of Jewel records, I get all the info from the Entity, Jewel.Birthstone is there, but it is null. However if I do a foreach on the results ---

foreach (Jewel j in jewels)
{
    string stone = jewelBusiness.RequestBirthstone(j.Birthmonth);
}

stone will equal the expected result (birthstone for that month).

Why doesn't my partial class return the birthstone??

Jazzy
  • 519
  • 9
  • 31
  • i think changing the get method of property and checking if the field is null, is the way to go. but remember you cant use extended properties to your entities in the entityframe-work query, so you must first use toList and then query the extended property. – Ashkan Ghodrat Apr 29 '13 at 08:57

3 Answers3

1

I'm not sure I understand your requirement correctly. But if you don't want to store Birthstone but calculate it on the fly, just change your code to

public partial class Jewel
{
    private string _birthstone;
    public string Birthstone
    {
        get 
        { 
             if (_birthstone == null)
             {
                  JewelBusiness jewelBusiness = new JewelBusiness();
                  _birthstone = jewelBusiness.RequestBirthstone(birthmonth); 
             }
             return _birthstone; 
        }
    }
}
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
0

Isn't your Jewels EntityObject in a partial class too? You can most likely just add a Jewels partial class to "extend" it and add the wanted property there.

Darko Kenda
  • 4,781
  • 1
  • 28
  • 31
  • That's what I was attempting to do, I'm not sure how to implement it, or if that is the best way for adding properties to an entity object before binding. – Jazzy Apr 29 '13 at 03:43
0

For me, it depends on where the logic for the calculated column resides.

If it resides in database, then you must do join query in the Linq. I assume in this case, you has a table named BirthStoneTable, with the month as the relation. I don't suggest to add a ternary operation inside linq query, such as select j.BirthDate.Month == 1 ? "Diamond" : //etc etc. It is hard to debug and to track (moreover for code coverage reason).

If it resides in UI specific (only to improve the display), I usually add a type-casted class, such as:

public class JewelUI{
  public explicit operator JewelUI(Jewel jewel){
    JewelUI jewelUI = new JewelUI();
    // assign birthdate and name
    jewelUI.BirthStone = GetBirthStone(jewel.BirthDate.Month);
  }

  public string BirthStone{get;set;};

  public string GetBirthStone(int month){
    if(month == 1) return "Diamond";
    //etc etc
  }
}

If the calculated column is used in the business logic, usually I handle the calculation in service / business logic. All of it to ensure the good Separation of Concern.

NB: I may misunderstand your requirement though

Fendy
  • 4,565
  • 1
  • 20
  • 25
  • I think you did understand, and your example is making sense. In a nutshell, I need an object that has properties from the database, and then some. – Jazzy Apr 29 '13 at 03:47