views:

92

answers:

2

I want to select all the fields of one table, and only specifics fields of a second table. Are there any way to do it like with table.* in SQL?

 var things = from t in db.table1
              from t2 in db.table2
              where ...Join Clause...
              select new { t.*,t2.onefield}

obviously this code don't work, but I think it ilustrate my pourpose.

if I use

select new {t,t2.onefield}

it compiles, but it fails when I assign it to a datasource of a datagridview. Currently I'm writing every field that I need of every table.

Thanks.

+1  A: 

That isn't good practice in SQL and, even if it can be done with Linq, wouldn't be good practice with Linq either, unless Linq would generate T-SQL code that expands to all the columns of the table.

Randy Minder
+1  A: 

One thing you could do is the following:

var things = from t in db.table1
          from t2 in db.table2
          where ...Join Clause...
          select new { MyProperty= t ,t2.onefield}

and then access T's fieds with:

var t= things.First();
Console.WriteLine("{0}, {1}, {2}", 
                  t.MyProperty.Field1, 
                  t.MyProperty.Field2, 
                  t.onefield);
Marcel Gosselin