How to execute one command conditionally based on the result of another? - system.reactive

I put together two commands in ReactiveUI: CheckNetwork and CheckVersion. Any time CheckNetwork executes (and it is executing at the correct times) I'd like to execute CheckVersion - but only if CheckNetwork returned "true".
ReactiveCommand<Unit, bool> CheckNetwork { get; }
ReactiveCommand<Unit, Unit> CheckVersion { get; }
CheckVersion = ReactiveCommand.CreateFromTask(CheckVersionImpl);
CheckNetwork = ReactiveCommand.CreateFromTask(CheckNetworkImpl);
I've tried a bunch of things so won't go through them all, just can't seem to get anything working...

This should do what you want
CheckNetwork
.Where(result => result)
.Select(_=> Unit.Default)
.InvokeCommand(CheckVersion);

Related

return custom object from stored procedure executed in EntityFramework, can't see object results

I am trying to execute a stored procedure using Entity Framework. I've tried the below and it returns the correct amount of rows, but when I look at the data in the debug window, it just shows my TYPE, it won't let me drill into to see what the actual values are.
SqlParameter param1 = new SqlParameter("#targetDate", filedate);
var result = db.Database.SqlQuery<PositionSheetCompResults>("dbo.comparePositionSheet #targetDate", param1);
Can anyone tell me how to do this?
here are the results it SHOULD return
here is the class
public class PositionSheetCompResults
{
public string AcctNum { get; set; }
You should write ToList<PositionSheetCompResults>();
var result = db.Database.SqlQuery<PositionSheetCompResults>("dbo.comparePositionSheet #targetDate", param1).ToList<PositionSheetCompResults>();

Running nunit test with specific data

I have a test that takes in test data. When using nunit console app to run the test, is there a way I can specify the data to be used?
Eg:
[Test, TestCaseSource(typeof(TestData))]
public void ATest(string param1, int param2)
public class TestData : IEnumerable
{
public IEnumerator GetEnumerator()
{
yield return new object[] { "blah1 blah1", 1};
yield return new object[] { "blah2 blah2", 2};
}
}
I want to be able to run ATest with test data ["blah2 blah2", 2] only. If I run as follows:
nunit3-console.exe Tests.dll --test=ATest --workers=1 --noresult
it will run twice.
Just run...
nunit3-console.exe Tests.dll --test ATest("blah2 blah2", 2)
or
nunit3-console.exe Tests.dll --where "test~=blah2"
If that string is unique to all your tests.
Note that the first one may require some escaping of the quotes, depending on your operating system.
One way to do this would be through returning a TestCaseData object instead.
Something like this: (untested, so syntax might be a little off!)
[Test, TestCaseSource(typeof(TestData))]
public void ATest(string param1, int param2)
public IEnumerator GetEnumerator()
{
yield return new TestCaseData("blah1 blah1", 1).SetName("FirstTest");
yield return new TestCaseData("blah2 blah2", 2).SetName("SecondTest");
}
To run the first test, you would then use the command line:
nunit3-console.exe Tests.dll --test=YourNameSpace.ATest.FirstTest --workers=1 --noresult
Depending what you're doing, setting the category may be more suitable than the name. The docs page shows what's available: https://github.com/nunit/docs/wiki/TestCaseData

Entity Framework field query slow

I'm having some trouble working with a particular ef query. I've simplified everything down as much as I can. I'm only querying for two columns. Here is my model.
[Table("TAXROLL", Schema = "CLAND")]
public class TaxRoll
{
[Key]
[Column("TAXROLL_ID")]
public string Id { get; set; }
[Column("APN")]
public string APN { get; set; }
}
When I execute my query in my controller, if I do firstordefault, the results take as long as 15-18 seconds to return. If I do a where query, the results are almost instantaneous (less than 1 second), (see my commented timing statements below. When I say 15-18 seconds and almost instantaneous, that's where my numbers are coming from).
[ResponseType(typeof(TaxRoll))]
public async Task<IHttpActionResult> Get(string id)
{
//var start = DateTime.Now;
//Debug.WriteLine("Starting Query");
var apnRecord = await ctx.TaxRoll.FirstOrDefaultAsync(x => x.APN == id);
//Debug.WriteLine("Returning APN after " + DateTime.Now.Subtract(start).TotalSeconds);
return Ok(apnRecord);
}
When I query for the primary key (Id), results return consistently fast every single time regardless of how I run the query. This is only a problem when I'm querying for APN. Yes, APN is indexed. It's also unique. I could use it as PK, and in fact I tried that. No dice. I know that executing a query that searches based on APN consistently returns fast when I do it directly against the database.
Any help or direction is greatly appreciated -- I am thoroughly confused.
Your APN Column is NULLABLE that makes EF add OR operator, 99% it makes SQL to "seek" the column (which does not use index). make APN column NOT NULL.
Additionally to the user skalinkin answer, you can set DbContextConfiguration.UseDatabaseNullSemantics property to true.
public class YourDbContext : DbContext
{
public YourDbContext()
{
Configuration.UseDatabaseNullSemantics = true;
// ...
}
}
The query that takes 15-18s
var apnRecord = await ctx.TaxRoll.FirstOrDefaultAsync(x => x.APN == id);
Is same as
var apnRecord = await ctx.TaxRoll.Where(x => x.APN == id).FirstOrDefaultAsync();
If you are using just Where(), nothing will be materialized from the database.
Also consider using Stopwatch instead of calculating timestamps.
var sw = new Stopwatch();
sw.Start();
// do something
Debug.WriteLine(sw.Elapsed);

Passing a comparison function and value as parameters into Entity Framework

This is a follow-up on question on Passing a comparison function as a parameter into Entity Framework.
I would like to pass a comparison function and a value into a search function that queries Entity Framework. The comparison function has to operate on different properties depending on the value. E.g., my data is
class DataItem
{
[Key]
public int ID { get; set; }
public bool Active { get; set; }
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
and I need a context method
public List<DataItem> SearchValue(Func<string, string, bool> op, string value)
{
if (value.Length < 10)
return DataItem.Where(di => di.Active && op(di.Prop1, value)).ToList();
else
return DataItem.Where(di => di.Active && op(di.Prop2, value)).ToList();
}
that I can then call like
List<DataItem> list = context.SearchValue((s1, s2) => s1 == s2, "A");
where I also need different comparison functions (all canonical).
Program compiles alright, but on running I get a "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities." error.
Loading the DataItems into memory is not an option since I'll have around a billion of them. That's why I'm looking for a server-side solution. All the operations I'm using are canonical, so they can be translated into SQL queries. My question is just: how can I pass them as parameters?
This issue means that your LINQ query cannot be converted to a proper SQL query for backend execution.All LINQ to SQL or Entity framework queries get converted to SQL which is run on the backend and if they have some operation which could not be translated to SQL this error can come.
I think you should first bring your data in memory using .ToList() or .AsEnumerable() and then use 'Op' operation on that.That should solve the problem
Unfortunately you can't do it with LinqToEntities. You have to create sql query manually or call Database function...

Is it possible to manipulate SOME entities in a ServiceOperation returning an IQueryable

I have a ServiceOperation to query items available to a certain user at a certain time.
In short this methode does:
var fullResult = from i in Items where ... select i; //get ALL possible items where...,
Lets say this returns items {A, B, C, D}.
A second query filters out which of those items the calling user has access to.
var clientResult = from ci in fullResult where (privilege's and schedule's are true)
This mite result in {A, C } and is returned.
The result on the client side is: only the list of items the client has access to is displayed. This can be annoying since you don't know if you made a mistake in searching, or the item is just not available right now.
What I would like to be able to do is show all possible results to the client {A, B, C, D} yet FLAG B and D in this case as unavailable.
My entity has already a property isReadOnly I could use.
Can I write a query to not just filter out, but also flag any remaining results as read only? An ideal result would be {A, B.isREadOnly=true, C, D.isReadOnly=true}
Or did I reach the limit of what is doable and do I have to write a traditional WCF web method, creating a separate class, returning a list of results?
PS: this 'isReadOnly' property is only used for this, I don't mind it being changed in the DB at all
Thanx for any pointers
Andreas
If I were you I would consider not returning the entity directly out of your service and instead map it to something that has the ReadOnly property. For example, if your entity is:
public class A
{
public string Name { get; set; }
}
Then you could have a data contract like this:
[DataContract]
public class AExtra
{
[DataMember]
public string Name { get; set; }
[DataMember]
public bool IsReadOnly { get; set; }
}
what this means is that you could do this in your query:
var res = from a
in Items
where (...whatever your original logic is)
select new AExtra
{
Name = a.Name,
IsReadOnly = (...put your logic for determining ReadOnly in here)
};
And then return res from your service operation.
Just an opinion really but I like to do things like this rather than send the entities directly out of the service - it always gives me a bit more freedom to change things without having too many knock-on effects.