Customized column in Linq
Two methods:
var books = _myIBookRepository.RetrieveAllBooks();
var bokexamples = _myIbokexamples Repository.RetrieveAllBookExample();
Two database table:
BookExample
- BokExampleID (primary key)
- BookID (Foreign key)
- OrderDetailID (Foreign key)
Book
- BookID (primary key)
- KategoryID (Foreign key)
- LanguageID (Foreign key)
- Title
BookID is a foreign key in table BookExample from table Book
If orderdetailID is NULL it means that this specfik bookexample is available in the store
Goal:
Display two column that is "Title" and "stock availiable". "stock availiable" is a customized column that don't exist in the database. "stock available" should contain numbers for instance 2 books of title .... is available.Problem:
Ha开发者_JS百科ving problem to find right source code in Linq in order to display these two column.// Fullmetalboy
var query = from b in books
select new
{
Title = b.Title,
StockAvailable = bookexamples.Count(be =>
be.BookID == b.BookID &&
be.OrderDetailID == null
)
};
If I understand your question right, this should be what you're looking for.
You could use another class that encapsulates your BookExample
class in a form that you want, e.g:
public class BookSummary
{
public int BookID {get; set; }
public int BookExampleID {get; set; }
public bool StockAvailable {get; set; }
public string Title {get; set; }
}
var books = from b in _myIbokexamples Repository.RetrieveAllBookExample().Include("Book") // I'm assuming that's there, lol
select new BookSummary
{
BookID = b.BookID,
BookExampleID = b.BookExampleID,
StockAvailable = b.OrderDetailID == null,
Title = b.Book.Title
};
精彩评论