1

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!

7
  • Your code snippet looks fine. Make sure you have using System.Linq; in order to use the .Union() function Commented Dec 19, 2017 at 18:24
  • is that worked for you ?? Commented Dec 19, 2017 at 18:25
  • Hi! I'm using System.Linq,I have this issue: "'IQueryable<int?>' does not contain a definition for 'Union and the best extension method overload 'ParallelEnumerable.Union<int>(ParallelQuery<int>, IEnumerable<int>)' requires a receiver of type 'ParallelQuery<int> ". Commented Dec 19, 2017 at 18:29
  • 2
    Looks like Oceny_przedmioty.ID_Studenta type is int? (nullable int) while Studenci.ID_Studenta just int. Parts of the Union has to have one and the same element type. Why don't you select the fields corresponding to your SQL query (which seem to be strings). Commented Dec 19, 2017 at 18:53
  • 2
    Replace select Oceny_przedmioty.ID_Studenta with select Oceny_przedmioty.ID_Studenta.Value Commented Dec 19, 2017 at 19:06

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your response! Unfortunately, where is Union line I have this error: "IQueryable<int?>' does not contain a definition for 'Union and the best extension method overload 'ParallelEnumerable.Union<int>(ParallelQuery<int>, IEnumerable<int>)' requires a receiver of type 'ParallelQuery<int>"
@AleksandraSkoczypiec - updated my answer can you please try that now
0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.