Passing an IQueryable as a parameter - c#

I have a class which I want to pass a list of data to. I want this class to
return a list of vendors in the whole set of data
filter the data by a condition which I have as a parameter and return the filtered results as a second enumerable list.
I have added IQueryable condition as pseudo-code for what I would like to achieve. Is this possible?
internal class Problem
{
public IEnumerable<Vendor> Vendors { get; set; }
public IEnumerable<Class1> ReturnedData { get; set; }
public Problem(List<Class1> data, IQueryable condition)
{
Vendors = data.SelectMany(d => d.Vendors).Select(v => v.VendorName).Distinct().Select(v => new Vendor {VendorName = v}).ToList();
ReturnedData = data.AsQueryable().Where(condition);
}
}
internal class Class1
{
public IEnumerable<Vendor> Vendors { get; set; }
// lots of other stuff
}
internal class Vendor
{
public object VendorName { get; set; }
}

I will do slightly differently.
First install nuget package System.Linq.Dynamic
Install-Package System.Linq.Dynamic
After that,
`IQueryable 's Where function will support string as parameter. So you can do this:
protected static IQueryable<T> ApplyGridFilter<T>(IQueryable<T> query)
{
var qText = "id == 1";
query = query.Where(qText);
return query;
}
The benefit is that you can then build up the where condition in clientside easily without creating Func<Class1, bool>>

When calling IQuerable<T>.Where() as an extension method, the parameter that it takes is not the IQueryable instance, but rather a predicate expression:
public Problem(List<Class1> data, Expression<Func<Class1, bool>> condition)
{
Vendors = data.SelectMany(d => d.Vendors).Select(v => v.VendorName).Distinct()
.Select(v => new Vendor {VendorName = v}).ToList();
ReturnedData = data.AsQueryable().Where(condition);
}
You can now create a variety of filters when you instantiate the Problem class:
var problemInstance = new Problem(data, x => x.VendorName == "Rob");

I would prefer using just a delegate as input parameter and implement the filter logic in the calling class
class Problem{
public Problem(data, Func<data, IEnumerable<Class1>> delegate){
ReturnedData = delegate(data);
}
}
var problem = new Problem(data, ds => ds.where(d => d.name =="xyz").tolist());

Related

MongoDB Driver C#, Search by nested property using filter definition

I need help with building filter for MongoCollection of class A when I have filters for class B
public class A
{
public string ExampleAProperty { get; set; }
public B NestedB { get; set; }
public ICollection<B> NestedBCollection { get; set; }
}
public class B
{
public string ExampleBProperty { get; set; }
}
public class SearchClass
{
public async Task<ICollection<A>> SearchAsync(IMongoCollection<A> collection)
{
// this is just simple example of possible dozens predefined filters for B class.
// see FilterProvider logic used now
// var bFilter = Builders<B>.Filter.Eq(x => x.ExampleBProperty, "Example");
// in need filter A where B meets provided filters
var cursor = await collection.FindAsync(
Builders<A>.Filter.And(
// predefined filters are easy to reuse with array of elements
Builders<A>.Filter.ElemMatch(x => x.NestedBCollection, FilterProvider.SearchValue("Oleh")),
// but i did not found how to do this with single nested element
Builders<A>.Filter.Eq(x => x.NestedB, FilterProvider.SearchValue("Oleh")) // how?
)
);
return await cursor.ToListAsync();
}
}
// statics is not good but just for working example :)
public static class FilterProvider
{
public static FilterDefinition<B> SearchValue(string? value)
{
var builder = Builders<B>.Filter;
// if value is null show all
if (string.IsNullOrEmpty(value))
{
return builder.Empty;
}
// if value is "Oleh" search for "Amir"
if (value == "Oleh")
{
value = "Amir";
}
// any other additional logic to compose proper filter
// this could be search by serveral properties and so on
// however just for example i will search by hashed value :)
value = value.GetHashCode().ToString();
return builder.Eq(x => x.ExampleBProperty, value);
}
}
Please DON'T
use IMongoQueryable
propose to filter nested element by duplicate code (like x => x.NestedB.ExampleBProperty = "something")
UPDATED: example for Amir with explanation why p.2 is not than case and why code will be is duplicated if you his approach :)
As you may see we have complex (but very simple in current example) filter of data in B class. If you will use your approach - logic of composing filter for B (specified in FilterProvider.SearchValue) will be duplicated.
Thank you
You can easily do this:
public async Task<ICollection<A>> SearchAsync(IMongoCollection<A> collection, string search)
{
var bFilter = Builders<B>.Filter.Eq(x => x.ExampleBProperty, search);
var cursor = await collection.FindAsync(
Builders<A>.Filter.And(
Builders<A>.Filter.ElemMatch(x => x.NestedBCollection, bFilter),
Builders<A>.Filter.Eq(x => x.NestedB.ExampleBProperty, search)
)
);
return await cursor.ToListAsync();
}

how to mock.SingleOrDefault() with Moq and C#

I've seen a few questions like this floating around but I'm looking for a good explination of how to get around this. I understand that Moq can't mock the extension call, but I'm just looking for a really good example. In the current code I there is a call like
var thing = listOfthings.myList.SingleOrDefault(lt => lt.Name== "NameToFind");
I've tried
MockedlistOfThings.Setup(x => x.myList.SingleOrDefault(o => o.Name == "NameToFind")).Returns(fakeObject);
Just looking for a good work around. thanks.
To further elaborate on how this situation came up, we are currently running a translation engine against large sets of data, that has to be run line by line. This translation engine passes in an Interface called IListOfthings. listOfthings is actually holding reference data in a dictionary that is preloaded in another call higher up in the program. I have created a "fakeObject" <- dictionary that holds my fake data that the method can use. I have Mocked the IlistOfthings which is passed in to the calling method. but I don't see how to fake the SingleOrDefault call.
Simplifed method below.
Public class ClassIMTesting
{
public void Translate(myObject obj, IlistOfThings listOfthings){
var thing = listOfthings.MyList.SingleOrDefault(lt => lt.Name== "NameToFind");
//Other logic here .....
}
}
public class Thing()
{
public string Name { get; set; }
public Dictionary MyDict { get; set; }
}
[TestFixture()]
public class MyCodeTest
{
MyObject myObj;
Mock<IListOfthings> listOfThings;
Thing thing;
[SetUp]
public void Setup()
{
myObj = new MyObject();
_thing = new thing();
_thing.Name = "MyName";
var myDict = new Dictionary<string, string>();
myDict.Add("70,~", "");
myDict.Add("70,145", "expectedResult");
myDict.Add("911,", "expectedResult");
thing.MyDict = myDict;
listOfThings = new Mock<IListOfthings>();
listOfThings.Setup(x => x.MyList.SingleOrDefault(o => o.Name == "MyName")).Returns(thing);
}
[TestCase("70", "~", "70070")]
[TestCase("70", "145", "expectedResult")]
[TestCase("911", "", "expectedResult")]
public void TranslateTest(string iTC, string ITCode, string expectedResult)
{
myObject.ITC = iTC;
myObject.ITCode = iTCode;
ClassIMTesting p = new ClassIMTesting();
p.Translate(myObject, listofThings.Object);
Assert.That(myObject.ITC3Code, Is.EqualTo(expectedResult));
}
}
public interface IListOfThings
{
List<Thing> MyList{ get; set; }
}
Given
public interface IListOfThings {
List<Thing> MyList { get; set; }
}
public class Thing() {
public string Name { get; set; }
public Dictionary MyDict { get; set; }
}
In order to provide a mock to satisfy the following example
public class ClassImTesting {
public Thing Translate(IlistOfThings listOfthings){
var thing = listOfthings.MyList.SingleOrDefault(lt => lt.Name== "NameToFind");
return thing
}
}
The mock just needs to return a collection that will allow the SingleOrDefault extension to behave as expected when invoked.
For example
//Arrrange
Mock<IListOfthings> listOfThings = new Mock<IListOfthings>();
var thing = new Thing {
Name = "NameToFind",
//...
};
List<Thing> list = new List<Thing>() { thing };
listOfThings.Setup(_ => _.MyList).Returns(list);
var subject = new ClassImTesting();
//Act
var actual = subject.Translate(listOfThings.Object);
//Assert
Assert.That(actual, Is.EqualTo(thing));
By having the mock return an actual List<Thing>, when
var thing = listOfthings.MyList.SingleOrDefault(lt => lt.Name== "NameToFind");
is invoked, the SingleOrDefault extension acts on a list where I can behave as expected.

system.collections.generic.list to system.collections.generic.ienumerable

I have a method where list cannot convert to ienumerable. How do i cast???
public ActionResult Attending()
{
var userId = User.FindFirst(ClaimTypes.NameIdentifier).Value;
var gigs = _context.Attendances.Include(a => a.Gig.Artist).Include(a => a.Gig.Genre).Where(a => a.AttendeeId == userId).ToList();
var viewModel = new GigsViewModel()
{
UpcomingGigs = gigs,
ShowActions = User.Identity.IsAuthenticated,
Heading = "Gigs I'm Attending"
};
return View("Gigs", viewModel);
}
Here is my ViewModel:
public class GigsViewModel
{
public IEnumerable<Gig> UpcomingGigs { get; set; }
public bool ShowActions { get; set; }
public string Heading { get; set; }
}
Try using below code, AsEnumerable() will do your work
var gigs = _context.Attendances.Where(a => a.AttendeeId == userId).select(a=>
new Gig()
{
property = a.property //assuming property, you can repeat the same for other properties of Gig
}).AsEnumerable();
The result of a Linq database query is typically IQueryable which is derived from IEnumerable, IQueryable, and IEnumerable.
In your case,it's linq database query so you need to call AsEnumerable();
You can also first fetch all attendees, and select the gigs.
IEnumerable<Gig> gigs = _context.Attendances.Where(a => a.AttendeeId == userId).Select(a => a.Gig).ToList();
Be aware if there can be multiple similar gigs, then perhaps you need only distinct set of gigs using Distinct:
IEnumerable<Gig> gigs = _context.Attendances.Where(a => a.AttendeeId == userId).Select(a => a.Gig).Distinct().ToList();
See more at: https://msdn.microsoft.com/en-us/library/bb534803(v=vs.110).aspx
Edited: edited the old suggestion, Distinct and Select added.

Hierarchical grouping data using linq - remove recursion

I found in this stackoverflow nice solution for hierarchical grouping.
How can I hierarchically group data using LINQ?
It helped me a lot, but I would like to ask how I can achieve the same result, but without recursion. Honestly I got a problem to convert it, because recursion is here natural approach for me. Anyone can convert this method to not use recursion?
Usage:
var result = customers.GroupByMany(c => c.Country, c => c.City);
Edited:
public class GroupResult
{
public object Key { get; set; }
public int Count { get; set; }
public IEnumerable<GroupResult> SubGroups { get; set; }
public override string ToString()
{ return string.Format("{0} ({1})", Key, Count); }
}
public static class MyEnumerableExtensions
{
public static IEnumerable<GroupResult> GroupByMany<TElement>(
this IEnumerable<TElement> elements,
params Func<TElement, object>[] groupSelectors)
{
if (groupSelectors.Length > 0)
{
var selector = groupSelectors.First();
//reduce the list recursively until zero
var nextSelectors = groupSelectors.Skip(1).ToArray();
return
elements.GroupBy(selector).Select(
g => new GroupResult
{
Key = g.Key,
Count = g.Count(),
SubGroups = g.GroupByMany(nextSelectors)
});
}
return null;
}
}
Thanks in advance
A bit challenging, but here you go:
public class GroupResult<T>
{
public object Key { get; set; }
public int Count { get; set; }
public IEnumerable<T> Items { get; set; }
public IEnumerable<GroupResult<T>> SubGroups { get; set; }
public override string ToString() { return string.Format("{0} ({1})", Key, Count); }
}
public static class MyEnumerableExtensions
{
public static IEnumerable<GroupResult<TElement>> GroupByMany<TElement>(
this IEnumerable<TElement> elements,
params Func<TElement, object>[] groupSelectors)
{
Func<IEnumerable<TElement>, IEnumerable<GroupResult<TElement>>> groupBy = source => null;
for (int i = groupSelectors.Length - 1; i >= 0; i--)
{
var keySelector = groupSelectors[i]; // Capture
var subGroupsSelector = groupBy; // Capture
groupBy = source => source.GroupBy(keySelector).Select(g => new GroupResult<TElement>
{
Key = g.Key,
Count = g.Count(),
Items = g,
SubGroups = subGroupsSelector(g)
});
}
return groupBy(elements);
}
}
The trick is to build the grouping lambda expressions in reverse order, using the previous step result and also using closure to capture the necessary information needed to execute the lambda.
The main question should be why do you want to get rid of the recursion from your implementation? The code you provided has max depth of recursion = groupSelectors.Length. I do not think stack usage should be your concern in that case.
The solution provided by Ivan is correct but I think it also has indirect recursive approach, and offers same stack consumption level. Instead of named declared method call, it builds the chain of nested delegate (Func) calls.
If your goal is to cheat some static code analysis tool (they usually do not like recursive calls) you can extract part of the GroupByMany into separate method and call one from another.

Select template

Is it possible to make a template for SELECT in a LINQ query? Right now I have 6 methods that uses the exact same SELECT, i would like to use a template if possible.
This is the code I'm using, when I want to make a change to the select I have to change the same thing at so many places in my code.
result = query.Select(b => new
{
route_id = b.b.route_id,
name = b.b.name,
description = b.b.description,
distance = b.b.distance,
distance_to_route = (int)b.distance_to_from_me,
departure_place = b.b.departure_place,
arrival_place = b.b.arrival_place,
owner = b.b.user.username,
average_rating = b.avg_rating,
is_favorite = b.is_favorite,
date = b.b.date,
attributes = b.b.route_attributes.Select(c =>
c.route_attribute_types.attribute_name),
coordinates = b.b.coordinates.Select(c =>
new coordinateToSend { sequence = c.sequence,
lat = c.position.Latitude,
lon = c.position.Longitude })
});
Here is a simple example of one way you could do this:
In your example, you're converting the source type to an anonymous type. You could create a class to represent your converted/result type, for example:
public class ResultClass
{
public string ResultPropA { get; set; }
}
For examples sake, lets say the following was the definition of your source class:
public class SourceClass
{
public string SourcePropA { get; set; }
}
Now that you have type definitions for your source and result objects, you can create an extension method to convert a collection of your source class to a collection of your result class:
public static class SourceToResultRepository
{
public static IEnumerable<ResultClass> ConvertSourceToResult
(this IEnumerable<SourceClass> source)
{
return source.Select(s => new ResultClass
{
ResultPropA = s.SourcePropA
//Add all other property transformations here
});
}
}
And here is an example of how you could use it wherever you need to perform the transformation:
//Extension usage:
var result = Database.Source.ConvertSourceToResult();
//Direct usage:
var result = SourceToResultRepository.ConvertSourceToResult(Database.Source);

Categories