Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all.
This is my situation.

I have 2 very similar queries that only change for the condition.

They are a_query and b_query

Now I need a third c_query query where the records of
b_query are not present in a_query.


The querys and the class:

a_query = (from A in  t009_prog.Where(x => (x.d_prog_f <= dData)) /* <- Contition */
     join B in  t011_reg on A.id_prog equals B.id_prog
      join C in  t005_per on B.id_per equals C.id_per into AB
        from D in AB.DefaultIfEmpty()
          join C in  t021_com on D.id_per equals C.id_per

           orderby A.id_prog, C.id_per, B.id_reg descending, C.id_com

        select new UTILITY.cl_PRIVACY
          {
            id_prog = A.id_prog,
            id_reg = B.id_reg,
            id_per = C.id_per,
            id_com = C.id_com
           }).Distinct().ToList();




            
b_query = (from A in  t009_prog.Where(x => (x.d_prog_f > dData || x.d_prog_f == null)) /* <- Contition */
    join B in  t011_reg on A.id_prog equals B.id_prog
      join C in  t005_per on B.id_per equals C.id_per into AB
        from D in AB.DefaultIfEmpty()
            join C in  t021_com on D.id_per equals C.id_per

             orderby A.id_prog, C.id_per, B.id_reg descending, C.id_com

           select new UTILITY.cl_PRIVACY
            {
              id_prog = A.id_prog,
              id_reg = B.id_reg,
              id_per = C.id_per,
              id_com = C.id_com
             }).Distinct().ToList();



    public class cl_PRIVACY
    {
        public int id_prog { get; set; }
        public int id_reg { get; set; }
        public int id_per { get; set; }
        public int id_com { get; set; }
    }


What I have tried:

For this aim I use "Contains" but I get the error message

Error CS1503 Argument 1: cannot convert from 'int' to 'UTILITY.cl_PRIVACY'


This is the query


c_query = (from p in a_query

       where !b_query.Contains(p.id_per)

     select new UTILITY.cl_PRIVACY
        {
          id_prog = A.id_prog,
          id_reg = B.id_reg,
          id_per = C.id_per,
          id_com = C.id_com
         }).Distinct().ToList();


Where is the error ? How can I resolve ?

Thanks in advance for your help. Bye
Posted
Updated 8-Dec-21 1:23am

1 solution

Both a_query and b_query are List<UTILITY.cl_PRIVACY>.

You are trying to test whether one of those lists contains an int. But it can't - the list contains instances of your class, not ints.

You're also referencing undeclared variables A, B, and C in your projection for c_query.

If you're using .NET 6, you can use the new ExceptBy method[^]:
C#
c_query = a_query.ExceptBy(b_query.Select(b => b.id_per), a => a.id_per).ToList();

For older versions, you'll need something like this:
C#
c_query = a_query.Where(a => !b_query.Any(b => b.id_per == a.id_per)).ToList();
 
Share this answer
 
Comments
antobaro 8-Dec-21 8:50am    
Thank you very much Richard !! ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900