2

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;

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

group clause or select clause expected
@baron - You get an exception with the above code? We need more information about what your data context looks like then I think.
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.
I used ado.net object mapping so its Database Entities
lol it was easier when I could just write my own direct sql query, far out

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.