1

first of all I am very new (noob). Probably my question is very stupid but I got stuck. Here is the situation.

I started programming a winforms database application using EF 5.0 . Everything is fine I can do what I want in forms like this:

private void Form1_Load(object sender, EventArgs e)
{        
    using (var ctx = new pehkEntities())
    {
        var result = from k in ctx.Kolons
                     select new
                     {
                         KolonAdı = k.KolonAdi,
                         En = k.En,
                         Boy = k.Boy,
                         Yükseklik = k.Yukseklik,
                         ID = k.KolonID
                     };


        dataGridView1.DataSource = result.ToList();
    }
}

But my form1 codes are really messed up right now and I want to clean it by using classes and methods. I tried to create a class and a method in program.cs but I have failed. How should I fix it?

public class veritabani_islemleri
{
    public ???? kolonlistele()
    {
    using(pehkEntities ctx = new pehkEntities())



        var result =from k in ctx.Kolons
               select new
               {
                   KolonAdı = k.KolonAdi,
                   En = k.En,
                   Boy = k.Boy,
                   Yükseklik = k.Yukseklik,
                   ID = k.KolonID
               };

        return ????
    }
}

My problem is I can not fill the question marks. I just want to do this in my form.

private void Form1_Load(object sender, EventArgs e)
{
   dataGridview.DataSource=myfunction();  
}

Or if my method is completely wrong how should I do this?

chaliasos
  • 9,659
  • 7
  • 50
  • 87
Hasan
  • 2,444
  • 3
  • 30
  • 44
  • 1
    check out this topic: http://stackoverflow.com/questions/1070526/how-to-return-anonymous-type-from-c-sharp-method-that-uses-linq-to-sql – MrO48V Sep 17 '12 at 11:14
  • possible duplicate of [LINQ to SQL: Return anonymous type?](http://stackoverflow.com/questions/534690/linq-to-sql-return-anonymous-type) – PHeiberg Sep 17 '12 at 11:20
  • possible duplicate of [Accessing C# Anonymous Type Objects](http://stackoverflow.com/questions/713521/accessing-c-sharp-anonymous-type-objects) – nawfal Jun 28 '14 at 12:54

2 Answers2

2

For this : You follow the below step:-

  1. Create the ViewModel Class
  2. And create the property which is reuired in forms
  3. Then Create a static method which will the property
  4. And then use that model in your form.

For Example :

public class  veritabani_islemleriModel{
 public String KolonAdi { get; set; }
 public String En { get; set; }
 public String Boy { get; set; }
 public String Yükseklik { get; set; }
 public String ID { get; set; }
}

and then 
public class veritabani_islemleri
{
    public List<veritabani_islemleriModel> kolonlistele()
    {
    using(pehkEntities ctx = new pehkEntities())



        var result =from k in ctx.Kolons
               select new veritabani_islemleriModel
               {
                   KolonAdı = k.KolonAdi,
                   En = k.En,
                   Boy = k.Boy,
                   Yükseklik = k.Yukseklik,
                   ID = k.KolonID
               };

        return result.Tolist();
    }
}
Quinton Bernhardt
  • 4,773
  • 19
  • 28
Pushpendra
  • 548
  • 3
  • 10
  • THX! this solved my problem and I've learned a lot. Now there is one more question. Since u don't know Turkish you defined my variables (En, Boy, Yukseklik) as String. They were int and I defined them as int. But it didn't work. There was an error message: "cannot convert int? to int" or smt. So I defined them as "int?". But why? in database they are ints but in C# "int?" is there something wrong? – Hasan Sep 17 '12 at 11:48
  • If you will see the database then it is null-able type that's why in entity frame work class the property type is int? so we have to declare int? type to our model. – Pushpendra Sep 17 '12 at 11:55
  • Yeah I did it and worked. You are right they are nullable. thx for the help! – Hasan Sep 17 '12 at 12:08
1

Your current Linq query is returning what is called an "anonymous type" (see here). At build time, the compiler is actually creating a class to hold the data for you that has all the right properties. However, C# can not return anonymous types from functions, which is why you don't know what type to return.

The best way to solve this is to make your own real class to hold the data, which is what Pushpendra does in their answer:

public class veritabani_islemleriModel{
 public String KolonAdi { get; set; }
 public String En { get; set; }
 public String Boy { get; set; }
 public String Yükseklik { get; set; }
 public String ID { get; set; }
}

Now you have a real type veritabani_islemleriModel that you can use in your method signature and return statement, instead of the compiler generated anonymous type.

CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138