Aleksandra Skoczypiec
Aleksandra Skoczypiec

Reputation: 91

SQL command into linq expression entity framework

I have 2 tables: Oceny_przedmioty and Studenci. Studenci and Oceny_przedmioty are in connection one - many (one student can have more than 1 grade). I need to make from this SQL :

SELECT Oprz_Ocena 
    FROM Oceny_przedmioty 
    UNION SELECT ST_Nr_indeksu 
          FROM Studenci 
          WHERE ST_Nr_indeksu = '11000'

linq expression what Visual Studio will understand. I work with entity framework. I tried something like this

   var currentGrade= (from Oceny_przedmioty in dbContext.Oceny_przedmioty
                            select Oceny_przedmioty.ID_Studenta).Union
                             (from Studenci in dbContext.Studenci
                              select Studenci.ID_Studenta);

but it doesn't even recognizes Union (does not contain a definition for Union). Thanks for any help in advance!

Upvotes: 1

Views: 237

Answers (2)

Aleksandra Skoczypiec
Aleksandra Skoczypiec

Reputation: 91

thank you all for your response! The problem also was with query and this code resolved all my problems

var query1 = dbContext.Oceny_przedmioty    
           .Join(dbContext.Studenci,
              post => post.ID_Studenta,        
              meta => meta.ID_Studenta,  
              (post, meta) => new { meta.ST_Nr_indeksu, post.OPrz_Ocena })
            .Where(postAndMeta => postAndMeta.ST_Nr_indeksu == 11000);    

       dataGridView1.DataSource = query1.ToList();

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176886

can you try like this. Include namespace using System.Linq; if not already done

var india = context.Orders.Where(o => o.ShipCountry == "India").Select(o => o);
var usa= context.Orders.Where(o => o.ShipCountry == "USA").Select(o => o);
var IndiaUnionusa = india.Union(usa);

for you code it will be like

 var quer1= (from Oceny_przedmioty in dbContext.Oceny_przedmioty
                            select Oceny_przedmioty.ID_Studenta);
 var query2 = (from Studenci in dbContext.Studenci
                              select Studenci.ID_Studenta);
  var currentGrade = query1.Union(query2);

seems like problem with int? nullable type

 var quer1= (from Oceny_przedmioty in dbContext.Oceny_przedmioty
                            select Oceny_przedmioty.ID_Studenta)
                            .ToList();
 var query2 = (from Studenci in dbContext.Studenci
                              select Studenci.ID_Studenta)
                             .ToList();
 var currentGrade = query1.Union(query2);

Upvotes: 2

Related Questions