1

I have linq queries but many times I need to edit the properties with some external methods as I did above, it gives expected result but the problem is it look so dirty eventhough I need to edit one column, I need to reconstruct entire object body as you see below,

  1. How can I make it better-looking?

  2. Whats the most efficient of achieve this?

  3. Is there any way to not duplicate the context for editing context?

(I feel like 1 answer covers all these questions)

var query = (from islem in dbContext
            join hasta in someEntity on islem.ID equals hasta.ID
            select new 
            {
                ID = islem.ID,
                Phone=hasta.Phone,
                BirthDate=islem.BirthDate,
                MuayeneSonucu= islem.TBMuayeneSonucuId,
                KurumKodu=islem.CreatedKurumKodu

            }).AsEnumerable().Select(s => new myCustomModel()
            {
                ID = s.ID,
                Birthdate=s.BirthDate.Date,
                Phone=FormatPhone(s.Phone),
                MuayeneSonucu = s.MuayeneSonucu,
                KurumAdi = getKurumAdiByKod(Convert.ToInt32(s.KurumKodu)) 
                // I need to recreate entire model because of this.. 
                // and s.KayitTarihi.Date property ofc
            }).AsQueryable().OrderByDescending(o => o.KayitTarihi);
adeveloper
  • 311
  • 1
  • 4
  • 14
  • where does `someEntity` come from, and what is the purpose of the join? you're not actually doing anything with `hasta` – Sam I am says Reinstate Monica Oct 18 '17 at 18:47
  • also, what type is `islem` Is it a `myCustomModel`? – Sam I am says Reinstate Monica Oct 18 '17 at 18:49
  • Why project into an anonymous type, then to `myNewCustomModel`? Just project directly to `myCustomModel` in the first `select`. – JuanR Oct 18 '17 at 18:50
  • thirdly a statement like , `from islem in dbContext` seems unusual to me. I usually expect to query from the `DbSet`s in `DbContext`, not from `DbContext` itself – Sam I am says Reinstate Monica Oct 18 '17 at 18:50
  • @SamIam I need an custom class object which contains some properties both these entities so I join them – adeveloper Oct 18 '17 at 18:52
  • You can change the `select new MyCustomModel()` within the first block and select the object directly from your database. However, there is a limit to code you can execute within code running on the database. For example, I see `getKurumAdiByKod()`. That would need to be converted to code that can run on the database. Also, the sorting should probably take place in your initial block using `orderby islem.???` for best efficiency. – Jonathan Wood Oct 18 '17 at 18:52
  • if you don't want to copy and paste your real code, then you can at least make a fully functional [SSCCE](http://sscce.org/) and include that. even small differences between the code in your question and actual code can have a significant impact on the answer – Sam I am says Reinstate Monica Oct 18 '17 at 18:53
  • @Juan dude this is why I post this entry linq dont allow me do that because I need to edit "KurumAdi" property with some method, so I need to use AsEnumerable before do that I wish I dont have do, And I ask here what should I do to handle this more efficient – adeveloper Oct 18 '17 at 18:54
  • You could implement a chainable `.ForEach` method that just yields each item after calling a function on it, similar to [these solutions](https://stackoverflow.com/a/2956199/382456). It's generally frowned upon for chain-style methods to cause side-effects, but it sounds like this is what you are asking for. – Scott Oct 18 '17 at 18:55
  • @JonathanWood yeah, thats what I try to accomplish here but can you eloborate it with some example and as answer so I can accept it. Expecially eloborate the thing about converting custom methods to database things like "getKurumAdiByKod" here – adeveloper Oct 18 '17 at 18:55
  • @user5570680: I cannot because you did not show what this method does. How can I talk about converting code that I have never seen? – Jonathan Wood Oct 18 '17 at 18:58
  • @user5570680: I see. What does `getKurumAdiByKod` do that LINQ doesn't like? – JuanR Oct 18 '17 at 19:02
  • @Juan It's `Entity Framework` that doesn't like it. Entity Framework converts your linq query to actual sql to run against the database, It just doesn't know how to convert arbitrary methods like `getKurumAdiByKod` to sql. – Sam I am says Reinstate Monica Oct 18 '17 at 19:05
  • @SamIam: I understand that. The question is what is the method doing? Maybe it's something he can do on the backend. Imagine if he is issuing another call to the database in there... – JuanR Oct 18 '17 at 19:38

1 Answers1

1

Option 1:

you can make your code look better by only selecting your tables

select new 
{
    islem = islem,
    hasta = hasta,
}

Option 2:

Depending on your schema, it might be possible to avoid the explicit join (and therefore the intermediate model) altogether by using navigation properties (EF will still do a db join behind the scenes)

Option 3:

You can also just slap a helper property on your model

class MyModel
{
    public int KurumAdi
    {
        ...
        get
        {
            return getKurumAdiByKod(Convert.ToInt32(this.KurumKodu));
        }   
    }
}