I have a dictionary Dictionary<string, List<string>> FilterDictionary which contains a column name as the key and the value of that key is a list of criteria that is going to be used as a where clause.
I need to query my database and for each column thats in the dictionary query that column with the criteria thats in the list for that key.
Just wondering what the best way to do this was?
This is what I would like to achieve but obviously I know its not as simple as columnName == filterCriteria.
var originalQuery = from p in productContext.Products
select p;
foreach (var item in FilterDictionary)
{
string columnName = item.Key;
foreach (var filterCriteria in FilterDictionary)
{
originalQuery.Where(columnName == filterCriteria);
}
}
I'd do something like this instead to hold your collection of filter conditions:
var filters = new List<Expression<Func<Product, Boolean>>>();
filters.Add(e => e.Name == "some name");
var originalQuery = from p in productContext.Products
select p;
originalQuery = filters
.Aggregate(originalQuery, (current, condition) => current.Where(condition));
At the end of the foreach loop, originalQuery will have logically ANDed together all of the criteria held in filters.
Related
I have a list of db table type I want to access list elements and compare with string but I cannot access list elements How to do it?
List<Tbl_UserCustomField> customattribute = (from custom in tniDataContext.Tbl_UserCustomFields
where workOrderIndex.LoginId == custom.LoginId
select custom).ToList();
After executing the query and storing the query result in list the customattribute is only returning count of elements in list but i want elements in string
Access it like:
foreach(var item in customattribute)
{
if(item.SomeField == "someString")
DoSomething();
else
DoSomethingElse();
}
If the varchar column is all you want then you can directly select it:
var customattribute = (from custom in tniDataContext.Tbl_UserCustomFields
where workOrderIndex.LoginId == custom.LoginId
select custom.SomeColumn).ToList();
foreach(var item in customattribute)
{
if(item == "someString")
DoSomething();
else
DoSomethingElse();
}
Try customattribute.ForEach(i => Console.Write("{0}\t", i)); and see what is showing on console?
If you know the column or property in Tbl_UserCustomField class which holds the string you are looking for, iterate through the customattribute and fetch the string. Basically, you will have to capture such result in a variable of type List<string>. This can be done in a plain foreach-way OR using a Select Linq expression that just retrieves the column you specify
// Approach 1:
List<string> customAttributeValues = customattribute.Select(c => c.YourStringFieldName).ToList();
// Approach 2:
List<string> customAttributeValues = new List<string>();
foreach (var custom in customattribute)
{
customAttributeValues.Add(custom.YourStringFieldName);
}
I have an
IEnumerable<typeA> result;
from this result I need to get sum group by some id.
So I have the query
var groupeddata = from data in result
group data by data.Title
into grouped
select new { intid= grouped.Key,
expsum= grouped.Sum(x=>x.expnum)};
now this expsum I need to assign to the items of result where typeA.id is same as intid. Now how to do this assignment?
The simplest approach would probably be to use a dictionary:
var sumDictionary = query.ToDictionary(pair => pair.intid, pair => pair.expsum);
foreach (var item in result)
{
// We don't know which property you actually want to assign to
item.Sum = sumDictionary[item.id];
}
Dictionary<int, string> lstSrc = new Dictionary<int, string>();
Dictionary<int, string> lstDest = new Dictionary<int, string>();
lstSrc.Add(1, "All");
lstSrc.Add(2, "Weekday");
lstSrc.Add(3, "WeekEnd");
lstDest.Add(1, "All");
lstDest.Add(2, "X1");
lstDest.Add(3, "X2");
lstDest.Add(4, "Weekday");
lstDest.Add(5, "WeekEnd");
Compare only when name matches in Source and Destination
var matchingItems = lstDest
.Where(l2 => lstSrc.Any(l1 => l1.Value.Equals(l2.Value))).ToList();
matchingItems.AddRange(lstDest.Except(matchingItems));
This query gives result as see in attached image how to get that result without using LINQ ?
How i can achieve this ?
[1]: http://i.stack.imgur.com/FLicZ.png
To get the matching items you could use a query like this:
var matchingItems = List2
.Where(l2 => List1.Any(l1 => l1.TimeGroupName.Equals(l2.TimeGroupName));
matchingItems.AddRange(List2.Except(matchingItems)));
Edited: equivalent without using Linq: (It's easy to forget how much boiler plate Linq saves you from writing!)
// Get the matching items
List<TIMEGROUPINFO> matchingItems = new List<TIMEGROUPINFO>();
foreach (TIMEGROUPINFO l1 in List1)
{
foreach (TIMEGROUPINFO l2 in List2)
{
if (l1.TimeGroupName.Equals(l2.TimeGroupName))
{
matchingItems.Add(l1);
continue;
}
}
}
// Append the items from List2 which aren't already in the list:
foreach (TIMEGROUPINFO l2 in List2)
{
bool exists = false;
foreach (TIMEGROUPINFO match in matchingItems)
{
if (match.TimeGroupName.Equals(l2.TimeGroupName))
{
// This item is already in the list.
exists = true;
break;
}
}
if (exists = false)
matchingItems.Add(l2);
}
I understand that you want to perform a query on list 2 based on list 1. Linq is very good for that.
so, if you wrote something like
//List1Element is a single element in the first list.
List1Element = List1[i];
List2.Where(l2 => l2.TimeGroupName == List1Element.TimeGroupName).ToList();
That might accomplish what I think you're trying to accomplish.
If you're trying to match the entire List1 at once, you can either iterate through all the list1 elements, or you can look into Linq Join operations
I have a datatable which contains a load of dates. I wanted to group these by date and give each row a count.
I have managed to do this by dong the following:
IEnumerable query = from row in stats.AsEnumerable()
group row by row.Field<string>("date") into grp
select new { Date = grp.Key, Count = grp.Count(t => t["date"] != null) };
(where "stats" is the datatable)
I can see from debugging that this brings back the values all grouped as I need, but now I need to loop them and get each date and count.
My problem is I don't know how to retrieve the values!
I have a foreach loop
foreach (var rw in query)
{
string date = rw.Date; // <---- this is my problem?
}
I don't know what type my Ienumerable is to be able to reference the values in it!
So my question is how can I retrieve each date and count for each row by doing similar to the above?
I hope this makes sense!
This link on my blog should help you
http://www.matlus.com/linq-group-by-finding-duplicates/
Essentially your type is an anonymous type so you can't reference it as a type but you can access the properties like you're trying to do.
I think I see your issue. If you're trying to return it from a method, you should define a type and reuturn it like shown below:
public IEnumerable<MyType> GetQuery()
{
var query = from row in stats.AsEnumerable()
group row by row.Field<string>("date") into grp
select new { Date = grp.Key, Count = grp.Count(t => t["date"] != null) };
foreach (var rw in query)
{
yield return new MyType(rw.Date, rw.Count);
}
}
declare your "query" variable using "var" as shown above.
I guess you don't have access to the properties of the anonymous class because you're using IEnumerable query = .... Try var query = ... instead.
Going by your comment "I am returning the query from a function", which I take to mean that you want to do the query in a method, return the data to the caller, and then iterate the data in the caller, I suggest you return a Dictionary<DateTime, int>, like this:
static Dictionary<DateTime, int> GetSummarisedData()
{
var results = (
from row in stats.AsEnumerable()
group row by row.Field<string>("date") into grp
select new { Date = grp.Key, Count = grp.Count(t => t["date"] != null) })
.ToDictionary(val => val.Date, val => val.Count);
return results;
}
then in the caller you can just
foreach (var kvp in GetSummarisedData())
{
// Now kvp.Key is the date
// and kvp.Value is the count
}
I have some code that groups a table by "Value1" and some loops that add the top row of each group to a list. This is a pretty ugly way to do this, and I was wondering if I could replace one of the foreach loops with a couple more lines in my LINQ query? Problem is, I don't have the foggiest idea how to do this.
var Result =
from a in DB.Table1
group new {Values = a} by a.Value1 into c
select new {everything = c};
foreach (var Row in Result)
{
foreach (var RowAll in Row.Everything)
{
List.Add(new List<string>() {RowAll.Value1, RowAll.Value2})
break;
}
}
Use Enumerable.First:
var List =
(from Row in Result
let RowAll = row.Everything.First()
select new List<string>() {RowAll.Value1, RowAll.Value2}
).ToList();
Or, combined with your original query:
var List =
(from a in DB.Table1
group a by a.Value1 into c
select new List<string>() {c.Key, c.First().Value2}
).ToList();
Use FirstOrDefault:
var query = from row in result
let rowAll = row.Everything.FirstOrDefault()
where rowAll != null
select new List<string> {rowAll.Value1, rowAll.Value2};
var list = query.ToList();
Rereading you first query I realised you are grouping on a property of 'a' so there should be no empty groupings. First() should be safe as in the other posters example.