Querying with LINQ basic question
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:
v开发者_Python百科ar 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;
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.
精彩评论