How do I select records and summary in one query? - linq

The scenario is like this:
name | val
'aa' | 10
'bb' | 20
'cc' | 30
*********
sum | 60
For now I just select all the records in simple LINQ query and invoke the enumerator (ToList())
Then I loop over the list and summarize the val column.
Is there a better way? LINQ selects all to a new typed object so I dont know how to add the additional data.
thanks.

Anonymous type cant allow value to be added or edited once its created. so instead of returning anonymous type, you can use your custom output class. Something like this
public class ResClass
{
public string name;
public int value;
}
public class OutClass
{
public int sum;
public List<ResClass> lstData;
}
int sum=0;
var outtt = objTT.Where(x => x.id == 1).Select(x =>
{
sum += x.value;
return new ResClass { name = x.name, value= x.value };
}).ToList();
OutClass outCls = new OutClass { sum = sum, lstData = outtt };

Related

enumerable group field using Linq?

I've written a Linq sentence like this:
var fs = list
.GroupBy(i =>
new {
X = i.X,
Ps = i.Properties.Where(p => p.Key.Equals("m")) <<<<<<<<<<<
}
)
.Select(g => g.Key });
Am I able to group by IEnumerable.Where(...) fields?
The grouping won't work here.
When grouping, the runtime will try to compare group keys in order to produce proper groups. However, since in the group key you use a property (Ps) which is a distinct IEnumerable<T> for each item in list (the comparison is made on reference equality not on sequence equality) this will result in a different collection for each element; in other words if you'll have two items:
var a = new { X = 1, Properties = new[] { "m" } };
var b = new { X = 1, Properties = new[] { "m" } };
The GroupBy clause will give you two distinct keys as you can see from the image below.
If your intent is to just project the items into the structure of the GroupBy key then you don't need the grouping; the query below should give the same result:
var fs = list.Select(item => new
{
item.X,
Ps = item.Properties.Where(p => p.Key == "m")
});
However, if you do require the results to be distinct, you'll need to create a separate class for your result and implement a separate IEqualityComparer<T> to be used with Distinct clause:
public class Result
{
public int X { get; set; }
public IEnumerable<string> Ps { get; set; }
}
public class ResultComparer : IEqualityComparer<Result>
{
public bool Equals(Result a, Result b)
{
return a.X == b.X && a.Ps.SequenceEqual(b.Ps);
}
// Implement GetHashCode
}
Having the above you can use Distinct on the first query to get distinct results:
var fs = list.Select(item => new Result
{
X = item.X,
Ps = item.Properties.Where( p => p.Key == "m")
}).Distinct(new ResultComparer());

Linq, force an item to be first?

I have a List<> of MyPersonObjects. The list is a list of people who I can assign something to. In this list, I include myself (As the task can be assigned to me - and usually is).
So, I want to make sure myself is at the top of the list. I know my personId, which is a property of my person object - so is there a way to order the list to make sure I am first, and then the rest, alphabetically by surname (which is another property of my object)
You can just order the list by two separate criteria, the OrderBy will be the main sort order, if the values are equal (1 for all except you), ThenBy is used.
personList.OrderBy(x => x.Id == myId ? 0 : 1)
.ThenBy(x => x.Surname);
One way is to sort the list by surname with Linq and then remove yourself and put back at the first place:
var personList = db.MyPersonObjects.OrderBy(p => p.Surname).ToList();
var myself = personList.Single(p => p.Id == myselfId);
personList.Remove(myself);
personList.Insert(0, myself);
Or create a custom comparer IComparer<MyPersonObject> where the implementation is something like:
public class PersonCompaper : IComparer<MyPersonObject>
{
private readonly int personId;
public PersonCompaper(int personId)
{
this.personId = personId;
}
public int Compare(MyPersonObject x, MyPersonObject y)
{
if (x.Id == personId && y.Id == personId)
return 0;
if (x.Id == personId)
return 1;
if (y.Id == personId)
return -1;
return string.Compare(x.Surname, y.Surname);
}
}
Then you use it in the OrderBy
db.MyPersonObjects.OrderBy(p => p, new PersonCompaper(myselfId))

Filtering Comma Separated Data

My site has a bunch of widgets and i'm trying to filter them based on the url which is passed in. Say a Widget has the following structure:
public class Widget {
public int Id { get; set; }
public string Name { get; set; }
public string Urls { get; set; }
}
Where Urls is a comma separated list for the urls where the widget should be displayed, e.g.:
/, /Blog/, /Blog/123, /News/*
The asterisk after News indicates the Widget will be selected whenever the passed in url starts with /News/.
How could i modify the following method to filter the widgets based on my conditions above?
public IList<Widget> GetWidgets(string url) {
return _session
.Where(w => w.Urls.Contains(url))
.ToList();
}
Ideally i'd like to use a linq query and it must only hit the database once. I'd appreciate the help. Thanks
I managed to solve this by adding my own wild card match generator. See http://sentinel101.wordpress.com/2010/12/30/extend-nhibernate-linq-for-regex-matching/ for example of how to register the generator. Here's the generator incase anyone is interested:
public class WildCardMatchGenerator : BaseHqlGeneratorForMethod {
public WildCardMatchGenerator() {
var methodDefinition = ReflectionHelper.GetMethodDefinition(() => WildCardMatchExtensions.WildCardMatch(null, null, ','));
SupportedMethods = new[] { methodDefinition };
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor) {
return treeBuilder.Equality(treeBuilder.MethodCall("[dbo].[WildCardMatch]", new[] {
visitor.Visit(arguments[0]).AsExpression(),
visitor.Visit(arguments[1]).AsExpression(),
visitor.Visit(arguments[2]).AsExpression()
}), treeBuilder.Constant(1));
}
}
And here is the WildCardMatch UDF:
CREATE FUNCTION [dbo].[WildCardMatch] (
#Pattern NVARCHAR(MAX),
#Input NVARCHAR(MAX),
#Separator NVARCHAR(5)
)
RETURNS BIT
AS
BEGIN
SET #Pattern = REPLACE(#Pattern, '*', '%')
DECLARE #RtnValue BIT
SELECT #RtnValue = CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END FROM [dbo].[Split](#Pattern, #Separator) WHERE #Input LIKE [Data]
RETURN #RtnValue
END
And the Split function it calls (from http://blogs.microsoft.co.il/blogs/itai/archive/2009/02/01/t-sql-split-function.aspx):
CREATE FUNCTION [dbo].[Split]
(
#RowData NVARCHAR(MAX),
#Separator NVARCHAR(MAX)
)
RETURNS #RtnValue TABLE
(
[Id] INT IDENTITY(1,1),
[Data] NVARCHAR(MAX)
)
AS
BEGIN
DECLARE #Iterator INT
SET #Iterator = 1
DECLARE #FoundIndex INT
SET #FoundIndex = CHARINDEX(#Separator, #RowData)
WHILE (#FoundIndex > 0)
BEGIN
INSERT INTO #RtnValue ([Data])
SELECT Data = LTRIM(RTRIM(SUBSTRING(#RowData, 1, #FoundIndex - 1)))
SET #RowData = SUBSTRING(#RowData, #FoundIndex + DATALENGTH(#Separator) / 2, LEN(#RowData))
SET #Iterator = #Iterator + 1
SET #FoundIndex = CHARINDEX(#Separator, #RowData)
END
INSERT INTO #RtnValue ([Data])
SELECT Data = LTRIM(RTRIM(#RowData))
RETURN
END
Lastly you'll need the C# implementation of the above UDF:
public static class WildCardMatchExtensions {
public static bool WildCardMatch(this string pattern, string input, char separator = ',') {
foreach (var str in pattern.Split(new char[] { separator }, StringSplitOptions.RemoveEmptyEntries)) {
if (Regex.IsMatch(input, Regex.Escape(str.Trim()).Replace("\\*", ".*")))
return true;
}
return false;
}
}
Hope this helps.
I don't think SQL server allows you to use IN for comma delimited field values. You need LIKE (SQL example below):
Urls LIKE '%_your_url_%'
Linq, Expressions, NHibernate and Like comparison may help you with translation LIKE to Linq to NHibernate

Linq query Group By multiple columns

I have a array of string say:
String[] Fields=new String[]{RowField,RowField1}
In which I can use the below query to get the values by specifying the values is query i.e RowField and RowField1:
var Result = (
from x in _dataTable.AsEnumerable()
select new
{
Name = x.Field<object>(RowField),
Name1 = x.Field<object>(RowField1)
})
.Distinct();
But if suppose I have many values in the Array like:
String[] Fields= new String[]
{
RowField,
RowField1,
RowField2,
.......
RowField1000
};
How can I use the query here without specifying each of the rowfield in the query?
How can i iterate through the array items inside the LINQ?
According to some suggestions in LINQ query and Array of string I am trying to get the result using the code below.
var result = (from row in _dataTable.AsEnumerable()
let projection = from fieldName in fields
select new {Name = fieldName, Value = row[fieldName]}
select projection.ToDictionary(p=>p.Name,p=>p.Value))
.Distinct();
But the problem is it does not return the distinct values.Any ideas?
Start with distinct DataRows by using this overload of Distinct():
_dataTable.AsEnumerable().Distinct(new DataRowEqualityComparer())
Where DataRowEqualityComparer is:
public class DataRowEqualityComparer : IEqualityComparer<DataRow>
{
public bool Equals(DataRow x, DataRow y)
{
return x.ItemArray.SequenceEqual(y.ItemArray);
}
public int GetHashCode(DataRow obj)
{
return string.Join("", obj.ItemArray).GetHashCode();
}
}

LINQ to Populate a range

I can't figure out how to do the second part of this (the for/foreach) with a LINQ expressions and haven't found any similar examples with LINQ. rangeDays will be between about 5 and 200, and q1 is a list of MyClasses where RowID is about from 10000 to 25000, without gaps.
public class MyClass { public int RowID; public object otherData; }
PopulateRange(int rangeDays, List<MyClass> q1){
var q2 = (from a in q1
let Rows = new int[rangeDays]
select new {a.RowID, Rows }).ToList();
foreach(var a in q2)
{
for(int i = 0; i < rangeDays; i++)
{
a.Rows[i] = a.RowID + i;
}
}
Thanks in advance.
Update:
I got this running with 2 linq statements as shown below (hopefully this is all runnable this time).
public List<MyClass> PopulateRange(int rangeDays, IQueryable<TheirClass> q1)
{
var q2 = (from a in q1
select new MyClass()
{ RowID = a.RowID, Rows = new int[rangeDays] }).ToList();
q2.ForEach(a => a.Rows = Enumerable.Range(0, rangeDays).
Select(i => i + a.RowID).ToArray());
return q2;
}
public class MyClass
{
public int RowID;
public int[] Rows;
}
public class TheirClass
{
public int RowID;
public int ID;
public string Symb;
public DateTime? EventTime;
public decimal? Value;
}
This is acceptable, but does anyone know why the following single statement throws a NotSupportedException "Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator." when I try to compile & run:
public List<MyClass> PopulateRange(int rangeDays, IQueryable<TheirClass> q1)
{
var q2 = (from a in q1
select new MyClass()
{ RowID = a.RowID, Rows = Enumerable.Range(0, rangeDays).
Select(i => i + a.RowID).ToArray() }).ToList();
return q2;
}
A slight variation on Ani's answer:
var q2 = q1.Select(a => new { Rows = Enumerable.Range(a.RowID, rangeDays)
.ToArray(),
RowID = a.RowID });
Differences:
When there's just a single select, I don't bother with query expression syntax
Rather than using Range from 0 and then Select, I figured it would be easier to just start off at a.RowID :)
I think q1's type should actually be List<MyRowClass> or similar (it certainly can't be a List<int>).
You probably want:
var q2 = (from a in q1
select new
{
a.RowID,
Rows = Enumerable.Range(0, rangeDays)
.Select(i => i + a.RowID)
.ToArray()
}).ToList();

Resources