LINQ group by expression syntax - c#

I've got a T-SQL query similar to this:
SELECT r_id, r_name, count(*)
FROM RoomBindings
GROUP BY r_id, r_name
I would like to do the same using LINQ. So far I got here:
var rooms = from roomBinding in DALManager.Context.RoomBindings
group roomBinding by roomBinding.R_ID into g
select new { ID = g.Key };
How can I extract the count(*) and r_name part?

Try this:
var rooms = from roomBinding in DALManager.Context.RoomBindings
group roomBinding by new
{
Id = roomBinding.R_ID,
Name = roomBinding.r_name
}
into g
select new
{
Id = g.Key.Id,
Name = g.Key.Name,
Count = g.Count()
};
Edit by Nick - Added method chain syntax for comparison
var rooms = roomBinding.GroupBy(g => new { Id = g.R_ID, Name = g.r_name })
.Select(g => new
{
Id = g.Key.Id,
Name = g.Key.Name,
Count = g.Count()
});

Related

How to use linq result input sql where in

I have a linq script
var ID = (from item in ConflictDatas.AsEnumerable()
group item by new
{
ID = item.Field<string>("ID"),
DesignArticle = item.Field<string>("DesignArticle"),
DesignNo = item.Field<string>("DesignNo"),
PatternCode = item.Field<string>("PatternCode")
} into g
where g.Count() >= 2
select new
{
g.Key.ID
}).ToList();
I want to put this result into a sql commnad.
I try:
string sqlwhere;
sqlwhere = string.Join(",", ID);
tsql = #"
Insert ConflictDesignArticle
Select * from ReadyworkData where ID in (" + sqlwhere + #") ";
After compile:
Insert ConflictDesignArticle
Select * from ReadyworkData where ID in ({ ID = SPSOS17040113 },{ ID =
SPSOS17040115 },{ ID = SPSOS17040114 })
How to modify my code. Thanks.
Thank you for Lei Yang help
var ID = (from item in ConflictDatas.AsEnumerable()
group item by new
{
ID = item.Field<string>("ID"),
DesignArticle = item.Field<string>("DesignArticle"),
DesignNo = item.Field<string>("DesignNo"),
PatternCode = item.Field<string>("PatternCode")
} into g
where g.Count() >= 2
select new
{
g.Key.ID
}).Select(x => x.ID).ToList();

C# Linq To Create Group On Multiple Properties and format the resultant group into single key/property?

I have following scenario where I want to find duplicates after forming the group and realign/format the duplicate data with some common class.
Example -
var lst = new List<Test>
{
new Test{Category="A",Class="Class1",Id="101",Name="John"},
new Test{Category="B",Class="Class2",Id="102",Name="Peter"},
new Test{Category="A",Class="Class2",Id="103",Name="David"},
new Test{Category="C",Class="Class3",Id="104",Name="Julia"},
new Test{Category="D",Class="Class4",Id="105",Name="Ken"},
new Test{Category="A",Class="Class1",Id="106",Name="Robert"},
};
I have created the group as -
var group =
from c in lst
group c by new
{
c.Category,
c.Class
} into g
select new
{
Category = g.Key.Category,
Class = g.Key.Class,
Id = lst.Where(x => g.Key.Category == x.Category && g.Key.Class==x.Class)
.Select(y => y.Id).ToList()
};
Which results me 2 group items for Category A with different Classes -
GroupItem1 - Category = "A" , Class = "Class1", Id = {101,106}
GroupItem2 - Category = "A" , Class = "Class2", Id = {103}
So I have requirement to show result in such case as below with other categories as -
Category = "A", Class = "Class1 OR SomeCommonClass", Id = {101,106,103}
Is it possible to achieve this result with minimum code and optimized logic.
If you want to group by Category and get the result below is the query.
var group =
from c in lst
group c by new
{
c.Category
} into g
select new
{
Category = g.Key.Category,
Class = lst.Where(x => g.Key.Category == x.Category).Select(y => y.Class).ToList(),
Id = lst.Where(x => g.Key.Category == x.Category)
.Select(y => y.Id).ToList()
};
Replace your group query with this:
var groups =
from c in lst
group c by c.Category into g
select new { Category = g.Key, Class = g.Select(c => c.Class).Distinct().Join(" or "), IDs = g.Select(c => c.Id).ToList() };
where Join is an IEnumerable extension method:
public static string Join(this IEnumerable<string> strings, string sep) => String.Join(sep, strings.ToArray());
var group = lst.GroupBy(l => l.Category)
.Select(x => new
{
Category = x.Key,
Class = string.Join(" OR ", x.Select(c => c.Class).Distinct()),
Ids = x.Select(c => c.Id).ToList()
}).ToList();

Linq Include Extra Column in GroupBy Selection

I have a "ReactionSimple" column that I'd like to include in the GroupBy selection but when I include it in the Group By it throws off the returned row count in a big way. Is there a way to include the commented out column in the final grouped selection without throwing off the result count. I'm a LINQ noob.
var reactivityResults = new List<ReactivityResultViewModel>();
var reactivityResults2 = new List<ReactivityResultViewModel>();
var classifications = GetMaterialClassifications(test.TestType);
var antigens = _db.Antigens.ToList();
if (test.ResultSets.Any())
{
var testResult = test.ResultSets.FirstOrDefault();
var dateReported = testResult.DateReported;
var details = testResult.ResultSetDetails.ToList();
if (details.Any())
{
reactivityResults = (from d in details
join a in _db.Antigens on d.AntigenId equals a.AntigenId
join ma in _db.MaterialAntigens on a.AntigenId equals ma.AntigenId
join md in _db.MaterialDefs on ma.MaterialDefId equals md.MaterialDefId
join c in classifications on md.MaterialId equals c.MaterialId
select new ReactivityResultViewModel
{
MaterialId = c.MaterialId,
MaterialName = c.MaterialName,
IsActive = md.IsActive,
ReactionSimple = (ReactionTypeEnum)d.ReactionSimple,
TestId = test.TestId,
AccessionId = test.AccessionId,
PatientFirstName = test.Patient.Party.NameFirst,
PatientLastName = test.Patient.Party.NameLast,
TestType = (TestTypeNoFlagsEnum)test.TestType,
DateReported = dateReported,
ManufacturerId = c.ManufacturerId,
ManufacturerName = c.ManufacturerName
})
.OrderBy(x => x.MaterialName)
.ToList();
var sreactivityResults2 = reactivityResults
.GroupBy(x => new {
x.MaterialId, x.AccessionId, x.DateReported,
x.IsActive, x.ManufacturerId, x.ManufacturerName,
x.MaterialName, x.PatientFirstName, x.PatientLastName,
x.TestId, x.TestType,
//x.ReactionSimple
})
.Select(x => new ReactivityResultViewModel
{
MaterialId = x.Key.MaterialId,
MaterialName = x.Key.MaterialName,
IsActive = x.Key.IsActive,
//ReactionSimple = x.Key.ReactionSimple,
TestId = test.TestId,
AccessionId = test.AccessionId,
PatientFirstName = test.Patient.Party.NameFirst,
PatientLastName = test.Patient.Party.NameLast,
TestType = (TestTypeNoFlagsEnum)test.TestType,
DateReported = dateReported,
ManufacturerId = x.Key.ManufacturerId,
ManufacturerName = x.Key.ManufacturerName
})
.ToList();
var sd2 = sreactivityResults2.ToList();
}
return reactivityResults;

Returning List value using linq C#

Actually I want to return the data from different lists based on Date. When i'm using this i'm getting data upto #Var result but i'm unnable to return the data. The issue with this is i'm getting error #return result. I want to return the data #return result. I'm using Linq C#. Can anyone help me out?
public List<CustomerWiseMonthlySalesReportDetails> GetAllCustomerWiseMonthlySalesReportCustomer()
{
var cbsalesreeport = (from cb in db.cashbilldescriptions
join c in db.cashbills on cb.CashbillId equals c.CashbillId
join p in db.products on cb.ProductId equals p.ProductId
select new
{
Productamount = cb.Productamount,
ProductName = p.ProductDescription,
CashbillDate = c.Date
}).AsEnumerable().Select(x => new ASZ.AmoghGases.Model.CustomerWiseMonthlySalesReportDetails
{
Productdescription = x.ProductName,
Alldates = x.CashbillDate,
TotalAmount = x.Productamount
}).ToList();
var invsalesreeport = (from inv in db.invoices
join invd in db.invoicedeliverychallans on inv.InvoiceId equals invd.InvoiceId
select new
{
Productamount = invd.Total,
ProductName = invd.Productdescription,
InvoiceDate = inv.Date
}).AsEnumerable().Select(x => new ASZ.AmoghGases.Model.CustomerWiseMonthlySalesReportDetails
{
Productdescription = x.ProductName,
Alldates = x.InvoiceDate,
TotalAmount = x.Productamount
}).ToList();
var abc = cbsalesreeport.Union(invsalesreeport).ToList();
var result = (from i in abc
group i by new { Date = i.Alldates.ToString("MMM"), Product = i.Productdescription } into grp
select new { Month = grp.Key, Total = grp.Sum(i => i.TotalAmount) });
**return result;**
}
You can either convert your result to a List before returning it using return result.ToList() or make your method return an IEnumerable<CustomerWiseMonthlySalesReportDetails> instead of List.
As your result is an enumeration of anonymous types you have to convert them to your CustomerWiseMonthlySalesReportDetails-type first:
select new CustomerWiseMonthlySalesReportDetails{ Month = grp.Key, Total = grp.Sum(i => i.TotalAmount) });
Assuming your type has exactly the members returned by the select.
EDIT: So your code should look like this:
var result = (from i in abc
group i by new { Date = i.Alldates.ToString("MMM"), Product = i.Productdescription } into grp
select new CustomerWiseMonthlySalesReportDetails{ Month = grp.Key, Total = grp.Sum(i => i.TotalAmount) });
return result.ToList();
You can assume Alldates property if is date of one of groups that month of date is in right place:
var result = (from i in abc
group i by new { Date = i.Alldates.ToString("MMM"), Product = i.Productdescription }
into grp
select new CustomerWiseMonthlySalesReportDetails{
Productdescription = grp.Key.Product,
TotalAmount = grp.Sum(i => i.TotalAmount),
Alldates =grp.First(i=>i.Alldates ) })
.ToList();

Translate SQL to lambda LINQ with GroupBy and Average

I spend a few hours trying to translate simple SQL to lambda LINQ
SELECT ID, AVG(Score) FROM myTable
GROUP BY ID
Any idea?
from t in myTable
group t by new {
t.ID
} into g
select new {
Average = g.Average(p => p.Score),
g.Key.ID
}
or Lambda
myTable.GroupBy(t => new {ID = t.ID})
.Select (g => new {
Average = g.Average (p => p.Score),
ID = g.Key.ID
})
The equivalent in Linq-to-Objects would be something like the below.
var results = from row in myTable
group row by row.Id into rows
select new
{
Id = rows.Key,
AverageScore = rows.Average(row => row.Score)
};
It's only slightly different for an ORM like entity framework. Namely, you would need to go through the data context or an appropriate DbSet/ObjectSet.
var _result = from a in myTable
group a by a.ID into g
select new
{
ID = g.Key.ID,
AverageResult = g.Average(x => x.Score)
}

Categories