Reputation: 2964
I am stuck on a LINQ query. Basically I have two tables linked by association on a uniqueID key. One table contains things that look duplicated (tblDupes), another is the fact table (tblVouchers). In the below query my 'sum' is working, but the FirstorDefault does not. It returns an error 'cannot implicitly convert type 'string' to 'bool'.
var dupeGroups = from d in db.tblDupes
group d by d.dupeGroup into g
select new
{
Group = g.Key,
TotalVal = g.Sum(v => v.tblVoucher.TransactionAmtDecimal),
Vendor = g.FirstOrDefault(x => x.tblVoucher.vendorName)
};
Any ideas? I've tried many different permutations of FirstOrDefault, putting it at the end, etc etc. I've also tried First().
Thanks!
Upvotes: 3
Views: 4318
Reputation: 17775
I think that the parameter to FirstOrDefault is a predicate used for filtering. You want to select the field you want to retrieve first, and then grab the first result (methinks) like this:
var dupeGroups = from d in db.tblDupes
group d by d.dupeGroup into g
select new {
Group = g.Key,
TotalVal = g.Sum(v => v.tblVoucher.TransactionAmtDecimal),
Vendor = g.Select(x => x.tblVoucher.vendorName).FirstOrDefault()
};
Hope this helps!
Upvotes: 3
Reputation: 77546
FirstOrDefault
takes a predicate, meaning a Func<T, bool>
. However, you are passing, x => x.tblVoucher.vendorName
, a Func<T, string>
, since vendorName
is a string. The bool
result is to indicate the first row that matches. For example, to find the first row with a vendorName
equal to "foo", you would pass in x => x.tblVoucher.vendorName == "foo"
.
Upvotes: 4