Unknown column error using Entity Framework and LINQ - c#

I started with EF yesterday and I am in trouble to transform this simple query into EF sintax
Translate:
select a.city from offer o, address a, offer_address oa
where o.identifier = oa.offeridentifier
and a.identifier = oa.addressidentifier
group by a.city
order by count(*) desc
Into:
var cities = (from o in db.offer
from a in db.address
from oa in db.offer_address
where (o.identifier == oa.offeridentifier
&& a.identifier == oa.addressidentifier)
group a by a.city into c
select new
{
quantity = c.Count(),
city = c.Key
}).OrderByDescending(a => a.quantity).Select(a => a.city);
var cityCollection = new List<string>();
foreach (var city in cities)
cityCollection.Add(city.ToString());
I have tried withou success
var cities = (from oa in db.offer_address
from of in db.offer.Where(x => x.identifier == oa.identifier)
from ad in db.address.Where(y => y.identifier == oa.offeridentifier).AsEnumerable()
group ad.city by new { ad.city } into g
select new
{
quantity = g.Count(),
city = g.Key
}).OrderByDescending(a => a.quantity);
The problem occurs when try to get inside the first loop!
Unknown column 'GroupBy1.K1' in 'field list'`
Line 55: foreach (var city in cities)`
With the second case:
Can't group on 'A1'
UPDATE
This code works, but its not i need
var cities = (
from of in db.offer
from ad in db.address
from oa in db.offer_address
where (of.identifier == oa.offeridentifier && ad.identifier == oa.addressidentifier)
group ad.city by new { ad.city } into g
select new
{
quantity = g.Count()
}).OrderByDescending(a => a.quantity)
Or
var cities = (
from of in db.offer
from ad in db.address
from oa in db.offer_address
where (of.identifier == oa.offeridentifier && ad.identifier == oa.addressidentifier)
group ad.city by new { ad.city } into g
select new
{
city = g.Key
});

Update - Try adding AsEnumerable() after the first select
var query = (from o in db.offer
from a in db.address
from oa in db.offer_address
where (o.identifier == oa.offeridentifier && a.identifier == oa.addressidentifier)
group a by a.city into c
select new
{
quantity = c.Count(),
city = c.Key
})
.AsEnumerable()
.OrderByDescending(a => a.quantity)
.Select(a => a.city);

Related

if control in linq?

This query is what I got:
var hede = (from customer in _customerRepository.Table
join source in _sourcedefinitionepository.Table on customer.SourceCode equals source.SourceCode
select new {Customer = customer, source.SourceName}
And then I wrote this:
if (agencyName ! = null )
hede = hede.Where(p => p.Customer.Name.StartsWith(agencyName));
How can i put the if code into first part of code?
You can achieve it in this way
where agencyName == null || customer.Name.StartsWith(agencyName));
Full query
var hede = (from customer in _customerRepository.Table
join source in _sourcedefinitionepository.Table on customer.SourceCode equals source.SourceCode
where agencyName == null || customer.Name.StartsWith(agencyName))
select new {Customer = customer, source.SourceName}
Updated
Using lamda.
var hede = _customerRepository.Table.Join(_sourcedefinitionepository.Table, c => c.SouceCode , s => s.SourceCode,
(c, s) => new
{
Customer = c,
s.SourceName
})).Where(p => agencyName == null || p.Customer.Name.StartsWith(agencyName)).ToList();
You can use the following code
var hede = _customerRepository.Table.Join(_sourcedefinitionepository.Table,
x => x.SourceCode,
y => y.SourceCode,
(customer, source) => new { customer, source.SourceName})
.Where(p => agencyName == null ||
(p.customer.Name.Any(f => p.customer.Name.StartsWith(agencyName))));
lambda code
var hede =from customer in _customerRepository.Table
join source in _sourcedefinitionepository.Table
on customer.SourceCode equals source.SourceCode
where agencyName == null || customer.Name.Any(f => customer.Name.StartsWith(agencyName))
select new { Customer = customer, source.SourceName };

Yet another “A query body must end with a select clause or a group clause”

This query does work, but I am trying to combine the two steps into one query.
var query1 = from b in db.GetTable<Boats>()
from o in db.GetTable<Offices>()
from u in db.GetTable<Users>()
.Where
(u =>
u.UserId == b.Handling_broker &&
o.Office == b.Handling_office &&
b.Status == 2 &&
officesToInclude.Contains(b.Handling_office)
)
select new
{
hOffice = o.Name,
bName = u.Name
};
var query2 = query1.GroupBy(t => new { office = t.hOffice, name = t.bName })
.Select(g => new { Office = g.Key.office, Name = g.Key.name, Count = g.Count() });
If I try to combine the two queries using the following query it gives me the “A query body must end with a select clause or a group clause” error.
var query1 = from b in db.GetTable<Boats>()
from o in db.GetTable<Offices>()
from u in db.GetTable<Users>()
.Where
(u =>
u.UserId == b.Handling_broker &&
o.Office == b.Handling_office &&
b.Status == 2 &&
officesToInclude.Contains(b.Handling_office)
)
.GroupBy(t => new { office = t.Office, name = t.Name })
.Select(g => new { Office = g.Key.office, Name = g.Key.name, Count = g.Count() });
I think I have to add a select something, but I can't figure out what.
Can anyone please help?
Your query must contain a select clause. The .Where(...).GroupBy(...).Select(...) are only on the db.GetTable<Users>(). Something like:
var query1 = from b in db.GetTable<Boats>()
from o in db.GetTable<Offices>()
from u in db.GetTable<Users>().Where(u => u.UserId == b.Handling_broker &&
o.Office == b.Handling_office &&
b.Status == 2 &&
officesToInclude.Contains(b.Handling_office))
.GroupBy(t => new { office = t.Office, name = t.Name })
.Select(g => new { Office = g.Key.office, Name = g.Key.name, Count = g.Count() })
select new { /* Desired properties */};
But I think you are looking for something like:
var result = from b in db.GetTable<Boats>()
from o in db.GetTable<Offices>()
from u in db.GetTable<Users>()
where u.UserId == b.Handling_broker &&
o.Office == b.Handling_office &&
b.Status == 2 &&
officesToInclude.Contains(b.Handling_office))
group 1 by new { t.Office, t.Name } into g
select new { Office = g.Key.Office, Name = g.Key.Name, Count = g.Count() };

Entity Framework Left Join on aggregate query

I have an EF query that contains a number of aggregate sub queries (Count, Sum and Max). There are two problems.
I need the joins on the sub queries to be left joins so that records are returned even if there are no results in the aggregate sub queries. At present, records are only returned if all sub queries return records.
The resulting list of WholesaleCustomerAndAggregateOrders objects that are returned contain a Contact object that needs to also include Addresses and Counties. I added Include(c => c.Addresses.Select(a => a.Country)) to the query but the Contact objects don't contain any Address objects.
Any assistance with either issue would be appreciated. Full query below.
var month1Date = DateTime.Today.AddMonths(-1);
var month3Date = DateTime.Today.AddMonths(-3);
var month6Date = DateTime.Today.AddMonths(-6);
var month12Date = DateTime.Today.AddMonths(-12);
var db = GetNewContext();
var qry = from c in db.Contacts
.Include(c => c.Addresses.Select(a => a.Country))
join orderCount in
(
from o in db.WholesaleOrders
group o by o.ContactId into g
select new
{
ContactId = g.Key,
TotalOrders = g.Count()
}
) on c.Id equals orderCount.ContactId
join month1Value in
(
from o in db.WholesaleOrders
where o.OrderDate >= month1Date
group o by o.ContactId into g
select new
{
ContactId = g.Key,
TotalValue = g.Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price))
}
) on c.Id equals month1Value.ContactId
join month3Value in
(
from o in db.WholesaleOrders
where o.OrderDate >= month3Date
group o by o.ContactId into g
select new
{
ContactId = g.Key,
TotalValue = g.Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price))
}
) on c.Id equals month3Value.ContactId
join month6Value in
(
from o in db.WholesaleOrders
where o.OrderDate >= month6Date
group o by o.ContactId into g
select new
{
ContactId = g.Key,
TotalValue = g.Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price))
}
) on c.Id equals month6Value.ContactId
join month12Value in
(
from o in db.WholesaleOrders
where o.OrderDate >= month12Date
group o by o.ContactId into g
select new
{
ContactId = g.Key,
TotalValue = g.Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price))
}
) on c.Id equals month12Value.ContactId
join month12Quantity in
(
from o in db.WholesaleOrders
where o.OrderDate >= month12Date
group o by o.ContactId into g
select new
{
ContactId = g.Key,
OrderCount = g.Count()
}
) on c.Id equals month12Quantity.ContactId
join lastOrderDate in
(
from o in db.WholesaleOrders
group o by o.ContactId into g
select new
{
ContactId = g.Key,
LastOrderDate = g.Max(r => r.OrderDate)
}
) on c.Id equals lastOrderDate.ContactId
select new WholesaleCustomerAndAggregateOrders
{
Contact = c,
TotalOrders = orderCount.TotalOrders,
Month1Value = month1Value.TotalValue,
Month3Value = month3Value.TotalValue,
Month6Value = month6Value.TotalValue,
Month12Value = month12Value.TotalValue,
Month12OrderCount = month12Quantity.OrderCount,
LastOrderDate = lastOrderDate.LastOrderDate
};
return await qry.ToListAsync();
How about this:
db.WholesaleOrders
.GroupBy(o => o.ContactId)
.Select(a => new {
a.Key,
TotalOrders = a.Count(),
LastOrderDate = a.Max(r => r.OrderDate),
Month1Value = a.Where(b => b.OrderDate >= month1Date).Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price),
Month3Value = a.Where(b => b.OrderDate >= month3Date).Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price),
Month6Value = a.Where(b => b.OrderDate >= month6Date).Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price),
Month12Value = a.Where(b => b.OrderDate >= month12Date).Sum(r => r.LineItems.Sum(l => l.QuantityOrdered * l.Price)
}).ToListAsync();
UPDATE: Add another property to projection:
Addresses = db.Addresses.Where(ad => ad.ContactId == a.Key);
where db is your context, and ad.ContactId is FK of the contact in Address table.
For this to work, multipleactiveresultsets property of the connection must be set to True.

what am i doing wrong in constructing Linq query

I am trying to write equivalent linq code for following query.
SELECT A.*
FROM
(
SELECT * FROM TableA
WHERE id = 100
) a
JOIN
(
SELECT Name, MAX(AnotherId) AnotherId
FROM TableA
WHERE id = 100
GROUP BY Name
) b
on a.Name = b.Name and a.AnotherId = b.AnotherId
This is the linq
var Collection = from R in DbContext.TableA
join G in (DbContext.TableA.Where(r => r.Id == 100).GroupBy(r => new { r.Name, r.AnotherId } ).Select(g => new { Name = g.Name , AnotherId = g.Max(o => o.AnotherId) }))
on new { R.Name, R.AnotherId } equals new { G.Name, G.AnotherId }
where R.Id == 100
select R;
But I am getting following compilation error that I don’t know how to fix. Any thoughts
The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.
Error 7 'System.Linq.IGrouping' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'System.Linq.IGrouping' could be found (are you missing a using directive or an assembly reference?)
You group by r.Name, r.AnotherId, when you just want to group by r.Name.
var Collection = from R in DbContext.TableA
join G in (DbContext.TableA
.Where(r => r.Id == 100)
.GroupBy(r => r.Name)
.Select(g => new { Name = g.Key , AnotherId = g.Max(o => o.AnotherId) }))
on new { R.Name, R.AnotherId } equals new { G.Name, G.AnotherId }
where R.Id == 100
select R;
And to have all in Fluent Syntax
var collection = DbContext.TableA
.Where(t1 => t1.Id == 100)
.Join(DbContext.TableA
.Where(t2 => t2.Id == 100)
.GroupBy(t2 => t2.Name)
.Select(group => new{Name = group.Key,
AnotherId = group.Max(e => e.AnotherId)})
),
t1 => new{t1.Name, t1.AnotherId} ,
t2 => new{t2.Name, t2.AnotherId},
(t1, t2) => t1);
llHi you need the following syntax, notice the addition of 'Key'
var Collection = from R in DbContext.TableA
join G in (DbContext.TableA.Where(r => r.Id == 100)
.GroupBy(r => new { r.Name, r.AnotherId } )
.Select(g => new { Name = g.Key.Name , AnotherId = g.Max(o => o.AnotherId) }))
on new { R.Name, R.AnotherId } equals new { G.Name, G.AnotherId }
where R.Id == 100
select R;
I would recommend using the query syntax for all parts of your query. By doing it this way, your linq query will have a more similar structure to your original sql query. It would look like this:
var query =
from a in
(from x in DbContext.TableA
where x.ID == 100
select x)
join b in
(from x in DbContext.TableA
where x.ID == 100
group x by x.Name into x
select new
{
Name = x.Key,
AnotherId = x.Max(o => o.AnotherId),
})
on new { a.Name, a.AnotherId } equals new { b.Name, b.AnotherId }
select a;
saj and Raphael both have found good points:
.GroupBy(r => new { r.Name, r.AnotherId } )
.Select(g => new { Name = g.Name , AnotherId = g.Max(o => o.AnotherId) }))
Groups don't have a Name. Each group has a Key (and the Key has a Name and AnotherId).
Since you want the Max(AnotherId), you don't want to include AnotherId in your grouping Key (same as it is not present in the GroupBy clause of the original query).
.GroupBy(r => r.Name) //the Name is the Key
.Select(g => new { Name = g.Key, AnotherId = g.Max(o => o.AnotherId) }))

How can i use unioan and nested from linq?

How can i convert to sql to linq . i don't understand nested select query. i need linq codes. Please help me:((((
SELECT #id=id FROM
(SELECT id,workorder AS workorderno ,CAST(jobseqno AS int) AS jobseqno FROM dbo.CTask
UNION ALL
SELECT id,workorderno,cardno AS jobseqno FROM dbo.NonRoutineCard
UNION ALL
SELECT id,workorderno,cardno AS jobseqno FROM dbo.AdditionalWorkCard) a
WHERE RTRIM(workorderno)=RTRIM(#ToNo) AND jobseqno=#ToSeq
LINQ:
var id =
(from task in stock.CTasks select new { task.id, task.workorder, task.jobseqno } )
.Union(from card in stock.NonRoutineCards select new { card.id, card.workorderno, card.cardno })
.Union(from card in stock.AdditionalWorkCards select new { card.id, card.workorderno, card.cardno })
.Where(x => x.workorderno.TrimEnd() == ToNo && x.jobseqno == ToSeq)
.FirstOrDefault();
if i write above codes error below:
will that suit your needs?
var id =
(from task in stock.CTasks select new { task.id, task.workorder, jobseqno= task.jobseqno } )
.Union(from card in stock.NonRoutineCards select new { card.id, card.workorderno, jobseqno = card.cardno })
.Union(from card in stock.AdditionalWorkCards select new { card.id, card.workorderno, jobseqno = card.cardno })
.Where(x => x.workorderno.TrimEnd() == ToNo && x.jobseqno == ToSeq)
.FirstOrDefault();

Categories