views:

62

answers:

1

I actually have two questions today, but they are both small :-)

1) Where is a good resource for learning linq/sql selections in C#?

2) Say I have a table called Model, with properties Name, Color, Shape. How do I do a query to select only the rows which DONT have null values in any column.

I've tried:

var d = database.Portfolio.SelectMany(x => x.Model.Model1 != null);

Thanks in advance

edits:

looks like var models = from p in database.Models.Where(x => x.Model1 != null) select p.Model1;

+4  A: 

I looked a lot at the the 101 LINQ Samples when I started with LINQ.

If you have a LINQ DataContext it's usually in the form [datacontext-name].[table]. So if your table is named Model it should be [datacontext-name].Model or in your case database.Model.

It's a little confusing that you are talking about a table named Model but in your code you are selecting from something called Portfolio. But in either way something like this should work.

var result = database.Model.Where(x=> x.Name != null && x.Color != null && x.Shape != null);

Edit By the look of the comments to this answer it looks like the problem is that you are mixing query mode and method mode when writing our query.

//Query mode
var models = from p in datDB.Models
             where p.Model1 != null
             select p;

//Method mode
var models = datDB.Models.Where(p => p.Model1 != null);

It is just different ways of writing a LINQ statement.

Jesper Palm
group clause or select clause expected
baron
@baron - You get an exception with the above code? We need more information about what your data context looks like then I think.
Jesper Palm
no exception it can't compile. jesper was right I have changed to: `var models = from p in datDB.Models.Where(x => x.Model1 != null)`. Problem is: A query body must end with a select clause or a group clause.
baron
I used ado.net object mapping so its Database Entities
baron
lol it was easier when I could just write my own direct sql query, far out
baron
@baron - Look at my updated answer.
Jesper Palm