how would i do a groupby in this instance ?[modified]
-
How would I do something like this
var distinctBooks = query
.GroupBy(x => x.BookID)
.Select(x => x.FirstOrDefault());to this
List<FillGrid> query = (from b in db.Books
join ba in db.BookAuthors on b.ID equals ba.BookID
join a in db.Authors on ba.AuthorID equals a.ID
join bn in db.BookNumbers on b.ID equals bn.BookID
where a.AuthorFirst == splitstring[1].ToString() && a.AuthorLast == splitstring[0].ToString()
select new FillGrid
{
AuthorID = a.ID,
Author = string.Format("{0} {1}", a.AuthorFirst, a.AuthorLast),
Title = b.Title,
Price = (decimal)b.Price,
BookNumber = bn.SurrogateNumber,
BookID = b.ID,
Count = db.BookNumbers.Count(c => c.BookID == b.ID)
}).ToList();I keep getting a conversion error... I also tried taking and doing query.tolist().firstordefault(); still no go any help would be great thanks
-
How would I do something like this
var distinctBooks = query
.GroupBy(x => x.BookID)
.Select(x => x.FirstOrDefault());to this
List<FillGrid> query = (from b in db.Books
join ba in db.BookAuthors on b.ID equals ba.BookID
join a in db.Authors on ba.AuthorID equals a.ID
join bn in db.BookNumbers on b.ID equals bn.BookID
where a.AuthorFirst == splitstring[1].ToString() && a.AuthorLast == splitstring[0].ToString()
select new FillGrid
{
AuthorID = a.ID,
Author = string.Format("{0} {1}", a.AuthorFirst, a.AuthorLast),
Title = b.Title,
Price = (decimal)b.Price,
BookNumber = bn.SurrogateNumber,
BookID = b.ID,
Count = db.BookNumbers.Count(c => c.BookID == b.ID)
}).ToList();I keep getting a conversion error... I also tried taking and doing query.tolist().firstordefault(); still no go any help would be great thanks
-
your first query works. should work. so should the second one. eventually try
var query = ...
instead of List... what's the splitstring?
I realize the first and second queries work that's not the issue. The issue is how do i do the first one using the data from the second one? Basically i want to use the query's results and only return the distinct ones or the firstordefault() so basically what I'm trying to do is
public class FillGrid
{
public int AuthorID {get; set;}
public int BookNumber {get;set;}
public int BookID { get; set;}
public int Count {get; set;}
public string Title { get; set; }
public string Author { get; set; }
public decimal Price { get; set; }
public string Keywords { get; set; }public List<FillGrid>FillTheGrid(string treeviewText,string treeviewTag) { List <FillGrid>results = new List<FillGrid>();
........
List<FillGrid> distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault());
return distinctresults;
</pre>but i keep getting the following error.
<pre>
Error 1 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<yetagain.DataOperations.FillGrid>' to 'System.Collections.Generic.List<yetagain.DataOperations.FillGrid>'. An explicit conversion exists (are you missing a cast?) -
I realize the first and second queries work that's not the issue. The issue is how do i do the first one using the data from the second one? Basically i want to use the query's results and only return the distinct ones or the firstordefault() so basically what I'm trying to do is
public class FillGrid
{
public int AuthorID {get; set;}
public int BookNumber {get;set;}
public int BookID { get; set;}
public int Count {get; set;}
public string Title { get; set; }
public string Author { get; set; }
public decimal Price { get; set; }
public string Keywords { get; set; }public List<FillGrid>FillTheGrid(string treeviewText,string treeviewTag) { List <FillGrid>results = new List<FillGrid>();
........
List<FillGrid> distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault());
return distinctresults;
</pre>but i keep getting the following error.
<pre>
Error 1 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<yetagain.DataOperations.FillGrid>' to 'System.Collections.Generic.List<yetagain.DataOperations.FillGrid>'. An explicit conversion exists (are you missing a cast?)well thats easy
//either repalce
List<FillGrid> distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault());
//with
List<FillGrid> distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault()).ToList();
//or with
var distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault());A LINQ query returns an IEnumerable by default and it does not get exectuted right away. Only when you do something with it. But if you call the .ToList() it will execute it and cache the results.
-
well thats easy
//either repalce
List<FillGrid> distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault());
//with
List<FillGrid> distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault()).ToList();
//or with
var distinctresults = query.GroupBy(i => i.BookID).Select(i => i.FirstOrDefault());A LINQ query returns an IEnumerable by default and it does not get exectuted right away. Only when you do something with it. But if you call the .ToList() it will execute it and cache the results.
yeah i just did that before you replied thanks again :)