This works fine:
var q = (from c in db.tblLiteCategorySpecs where CategoryIDs.Contains(c.CategoryID) select c.SpecID).Distinct().ToList();
var qq = (from s in db.tblSpecifications where q.Contains(s.id) select s);
However I now need to return another field in the first query:
var q = (from c in db.tblLiteCategorySpecs where CategoryIDs.Contains(c.CategoryID) select new { c.SpecID, c.FriendlyName }).Distinct().ToList();
var qq = (from s in db.tblSpecifications where q.Contains(s.id) select s);
So the q.contains now fails, I need this to somehow work on the q query SpecID field. Anyone know how to do this?
Well, you could try:
var qq = from s in db.tblSpecifications
where q.Select(x => x.SpecID).Contains(s.id)
select s;
In other words, project the result before using Contains. I've no idea what the SQL will look like.
By the way, I'd personally just write this as:
var qq = db.tblSpecifications
.Where(s => q.Select(x => x.SpecID).Contains(s.id));
I only use query expression syntax where it really makes things simpler. I'd also strongly encourage you to use multiple lines for the queries - it can really help with readability.
Is this what you want?
var q = (from c in db.tblLiteCategorySpecs where CategoryIDs.Contains(c.CategoryID) select new { c.SpecID, c.FriendlyName }).Distinct().ToList();
var qq = (from s in db.tblSpecifications where q.Any(c => c.SpecID == s.id) select s);
Related
this is my query in sql server and everything works fine
select * from DetalleNotas
order by len(ColProduct), ColProduct
PROCT1
PROCT2
PROCT3
PROCT4
PROCT5
PROCT6
PROCT7
PROCT8
PROCT9
PROCT10
but i want my query in linq c#
I tried this and it does not work
var product = (from d in db.Product
orderby len(d.ColProduct), d.ColProduct
select new
{
product= d.product
});
the name "len" does not exist in the real context
only this query works
var product = (from d in db.DetalleNotas
orderby d.ColProduct
select new
{
product= d.product
});
This is the result of my functional query
PROCT1
PROCT10
PROCT2
PROCT3
PROCT4
PROCT5
PROCT6
PROCT7
PROCT8
PROCT9
Remember that in C# Linq code, everything is still C#. As you're working with strings you need to order by string.Length. For example:
var results = from d in db.DetalleNotas
orderby d.ColProduct.Length
select d;
You could order it by the number on the end of your strings:
var result = db.DetalleNotas.OrderBy(d => Convert.ToInt32(d.ColProduct.Substring(5)))
.ToArray();
I have a very simple SQL
SELECT s.shop_code
,SUM(im.amt) sum_amt
,s.cell_no#1 shop_cell
FROM tb_sn_so_wt_mst im
,tb_cm_shop_inf s
WHERE im.shop_code = s.shop_code
GROUP BY s.shop_code, s.cell_no#1)
then i try to code linq
var listResult = from warrantyMaster in listWarrantyMasters2.Records
join shopInfo in listShopInfos
on warrantyMaster.ShopCode equals shopInfo.ShopCode
i don't know group by shop code and cell no and sum atm, any one help me out of this problem
The group by syntax with some examples is explained here group clause (C# Reference) and related links.
Here is the direct translation of your SQL query (of course the field names are just my guess since you didn't provide your classes):
var query = from im in listWarrantyMasters2.Records
join s in listShopInfos
on im.ShopCode equals s.ShopCode
group im by new { s.ShopCode, s.CellNo } into g
select new
{
g.Key.ShopCode,
g.Key.CellNo,
SumAmt = g.Sum(e => e.Amt)
};
You can try this code:
var results = from warrantyMaster in listWarrantyMasters2.Records
from shopInfo in listShopInfos
.Where(mapping => mapping.ShopCode == warrantyMaster.ShopCode )
.select new
{
ShopCode = warrantyMaster.ShopCode,
ATM = listWarrantyMasters2.ATM,
ShellNo = shopInfo.ShellNo
}
.GroupBy(x=> new { x.ShopCode, x.ShellNo })
.Select(x=>
new{
ShopCode = x.Key.ShopCode,
ShellNo = x.Key.ShellNo,
SumATM = x.Sum(item=>item.ATM)
});
I am having following query in sql :
SELECT [definition],[pos]
FROM [WordNet].[dbo].[synsets]
where synsetid in(SELECT [synsetid] FROM [WordNet].[dbo].[senses]
where wordid = (select [wordid]FROM [WordNet].[dbo].[words]
where lemma = 'searchString'))
I had tried this for sql to linq :
long x = 0;
if (!String.IsNullOrEmpty(searchString))
{
var word = from w in db.words
where w.lemma == searchString
select w.wordId;
x = word.First();
var sence = from s in db.senses
where (s.senseId == x)
select s;
var synset = from syn in db.synsets
where sence.Contains(syn.synsetId)
select syn;
But I am getting following error at sence.Contains()
Error1:Instance argument: cannot convert from
'System.Linq.IQueryable<WordNetFinal.Models.sense>' to
'System.Linq.ParallelQuery<int>'
Below code:
var sence = from s in db.senses
where (s.senseId == x)
select s;
Returns object of type: WordNetFinal.Models.sense, but in where sence.Contains(syn.synsetId) you are trying to search in it syn.synsetId which is an integer.
So you should change above code to:
var sence = from s in db.senses
where (s.senseId == x)
select s.senseId;
x seems to be of Word type, which is not the type of Id (probably int or long).
You're comparing an entire sense row with a synsetId, which is not correct. You're also splitting the original query into two separate queries by using First() which triggers an evaluation of the expression so far. If you can live with not returning an SQL error if there are duplicates in words, you can write the query as something like this;
if (!String.IsNullOrEmpty(searchString))
{
var wordIds = from word in db.words
where word.lemma == searchString
select word.wordId;
var synsetIds = from sense in db.senses
where wordIds.Contains(sense.wordId)
select sense.synsetId;
var result = (from synset in db.synsets
where synsetIds.Contains(synset.synsetId)
select new {synset.definition, synset.pos}).ToList();
}
The ToList() triggering the evaluation once for the entire query.
You could also just do it using a simpler join;
var result = (from synset in db.synsets
join sense in db.senses on synset.synsetId equals sense.synsetId
join word in db.words on sense.wordId equals word.wordId
select new {synset.definition, synset.pos}).ToList();
Basically I'm trying to do this in LINQ to SQL;
SELECT DISTINCT a,b,c FROM table WHERE z=35
I have tried this, (c# code)
(from record in db.table
select new table {
a = record.a,
b = record.b,
c = record.c
}).Where(record => record.z.Equals(35)).Distinct();
But when I remove column z from the table object in that fashion I get the following exception;
Binding error: Member 'table.z' not found in projection.
I can't return field z because it will render my distinct useless. Any help is appreciated, thanks.
Edit:
This is a more comprehensive example that includes the use of PredicateBuilder,
var clause = PredicateBuilder.False<User>();
clause = clause.Or(user => user.z.Equals(35));
foreach (int i in IntegerList) {
int tmp = i;
clause = clause.Or(user => user.a.Equals(tmp));
}
var results = (from u in db.Users
select new User {
a = user.a,
b = user.b,
c = user.c
}).Where(clause).Distinct();
Edit2:
Many thanks to everyone for the comments and answers, this is the solution I ended up with,
var clause = PredicateBuilder.False<User>();
clause = clause.Or(user => user.z.Equals(35));
foreach (int i in IntegerList) {
int tmp = i;
clause = clause.Or(user => user.a.Equals(tmp));
}
var results = (from u in db.Users
select u)
.Where(clause)
.Select(u => new User {
a = user.a,
b = user.b,
c = user.c
}).Distinct();
The ordering of the Where followed by the Select is vital.
problem is there because you where clause is outside linq query and you are applying the where clause on the new anonymous datatype thats y it causing error
Suggest you to change you query like
(from record in db.table
where record.z == 35
select new table {
a = record.a,
b = record.b,
c = record.c
}).Distinct();
Can't you just put the WHERE clause in the LINQ?
(from record in db.table
where record.z == 35
select new table {
a = record.a,
b = record.b,
c = record.c
}).Distinct();
Alternatively, if you absolutely had to have it the way you wrote it, use .Select
.Select(r => new { a = r.a, b=r.b, c=r.c }).Distinct();
As shown here LINQ Select Distinct with Anonymous Types, this method will work since it compares all public properties of anonymous types.
Hopefully this helps, unfortunately I have not much experience with LINQ so my answer is limited in expertise.
I have something like this:
var itemsInCart = from o in db.OrderLineItems
where o.OrderId == currentOrder.OrderId
select new { o.OrderLineItemId, ..., ..., o.WishListItem.Price}
is there any way to do a
itemsCart.Sum() //not sure what to pass into the function
to get the sum of o.WishListItem.Price or do i have to get another iQueryable< T> from the database with group ... into?
What about:
itemsInCart.AsEnumerable().Sum(o=>o.Price);
AsEnumerable makes the difference, this query will execute locally (Linq To Objects).
you can:
itemsCart.Select(c=>c.Price).Sum();
To hit the db only once do:
var itemsInCart = (from o in db.OrderLineItems
where o.OrderId == currentOrder.OrderId
select new { o.OrderLineItemId, ..., ..., o.WishListItem.Price}
).ToList();
var sum = itemsCart.Select(c=>c.Price).Sum();
The extra round-trip saved is worth it :)
Try:
itemsCard.ToList().Select(c=>c.Price).Sum();
Actually this would perform better:
var itemsInCart = from o in db.OrderLineItems
where o.OrderId == currentOrder.OrderId
select new { o.WishListItem.Price };
var sum = itemsCard.ToList().Select(c=>c.Price).Sum();
Because you'll only be retrieving one column from the database.
I know this is an old question but why can't you do it like:
db.OrderLineItems.Where(o => o.OrderId == currentOrder.OrderId).Sum(o => o.WishListItem.Price);
I am not sure how to do this using query expressions.
Try this:
var itemsInCart = from o in db.OrderLineItems
where o.OrderId == currentOrder.OrderId
select o.WishListItem.Price;
return Convert.ToDecimal(itemsInCart.Sum());
I think it's more simple!