I'm trying to run an integer comparison (for instance greater than or less than) using OData on a value that is set to be a type of varchar in the database. Crossing out the solution of changing the database field to be a type of int, as it's not preferred in my specific case, is there a way to tell Telerik OpenAccess to convert the field to a type of integer when executing either the query or the mappings?
Thanks in advance.
It is similar with this one: Handling Dates with OData v4, EF6 and Web API v2.2
Add one more proerpty:
public partial class Title
{
public int Id { get; set; }
public string Name { get; set; }
public string StringProperty { get; set; }
}
public partial class Title
{
[NotMapped]
public int IntProperty
{
get
{
return StringProperty==null?0;Int32.Parse(StringProperty);;
}
set
{
StringProperty = value.ToString();
}
}
}
Update the model:
public static IEdmModel GetModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
EntityTypeConfiguration<Title> titleType= builder.EntityType<Title>();
titleType.Ignore(t => t.StringProperty);
titleType.Property(t => t.IntProperty ).Name = "MyProperty";
builder.EntitySet<Title>("Titles");
builder.Namespace = typeof(Title).Namespace;
return builder.GetEdmModel();
}
Related
I have an SQlite table with 4 Columns in Xamarin app.
I've already inserted 3 columns and now i need an update statement to Update 4th column with some values using for loop.
(OR)
Please suggest any better/other method to do the same.
Do you want to update the record in the sqlite DB?
If so you can use this model to update the record in the DB.prijem.BCode is PrimaryKey, we set the type of PrimaryKey is int and AutoIncrement, So, if the model's PrimaryKey is not equal zero, this record stored in the DB, we can update this record by the Model.
readonly SQLiteAsyncConnection _database;
public PrijemDatabase(string dbPath)
{
_database = new SQLiteAsyncConnection(dbPath);
_database.CreateTableAsync<Prijem>().Wait();
}
public Task<int> SavePrijemAsync(Prijem prijem)
{
if (prijem.BCode != 0)
{
return _database.UpdateAsync(prijem);
}
else
{
return _database.InsertAsync(prijem);
}
}
Here is my model.
public class Prijem
{
[PrimaryKey, AutoIncrement, Unique]
public int BCode { get; set; }
public string Name { get; set; }
public string FirmName { get; set; }
public string ItemCode { get; set; }
public string Count { get; set; }
}
Here is link about how to execute CRUD, you can refer to it.
https://learn.microsoft.com/en-us/xamarin/get-started/quickstarts/database?pivots=windows
Here is a demo about it.
https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/getstarted-notes-database/
There are two tables "Task" and "Client". Both this tables are related with ClientId (foreign key).
In this query I'm trying to get the client name based on ClientId from Task table with $expand keyword. Below is the query and Entity classes.
OData Query : http://localhost:52484/Task?$expand=Client($select=Name)
public class Task: GeneralTask
{
public Task() { }
public Task(
int clientId,
string title,
)
{
this.Title = title;
this.ClientId = clientId;
}
}
public abstract class GeneralTask
{
protected GeneralTask()
{
}
public string Title { get; set; }
public int ClientId { get; set; }
public virtual Client Client { get; set; }
}
But I get the below error.
Error Message :"The query specified in the URI is not valid. The property 'Client' cannot be used in the $expand query option."
Any help would be appreciated.
We need to enable OData Model Bound Attributes which you can do globally with the middle line in the following block(WebApiConfig.cs file)
ODataModelBuilder builder = new ODataConventionModelBuilder();
config.Count().Filter().OrderBy().Expand().Select().MaxTop(null); //new line
builder.EntitySet<DB.Project>("Projects");
Here is controller code
[EnableQuery]
public IQueryable<Product> Get()
{
var productRepository = new ProductRepository();
return productRepository.Retrieve().AsQueryable();
}
Here is Retrieve() method
internal List<Product> Retrieve()
{
var filePath = HostingEnvironment.MapPath(#"~/App_Data/product.json");
var json = System.IO.File.ReadAllText(filePath);
var products = JsonConvert.DeserializeObject<List<Product>>(json);
return products;
}
And Product class
public class Product
{
public string Description { get; set; }
public decimal Price { get; set; }
public string ProductCode { get; set; }
public int ProductId { get; set; }
public string ProductName { get; set; }
public DateTime ReleaseDate { get; set; }
}
Other filters like $filter=Price+gt+6 or $top=4 and $skip=1 work fine. WebApi.OData package version=5.7.0
Error:
"The query specified in the URI is not valid. An unknown function with name 'contains' was found.
substringof() is a V3 function while contains() is a V4 function.
Try contains:
$filter=contains(Name,'value')
You are probably using an OData library package for OData version 3, but contains is a version 4 function. You can either query with the substringof function defined in version 3, or switch to a package that supports OData version 4.
I create a project where I use EF with LINQ and Model first. So, based on my edmx I created my Database and also my classes.
So I got some problems. I created a Click to test if my code is working.
protected void btnSearch_Click(object sender, EventArgs e)
{
ZUser Zusr = new ZUser();
List<ZUser> lst = Zusr.ListAll();
// Zusr.Id = 1;
string test = "";
foreach (ZUser item in lst)
{
test = item.Name;
}
lblName.Text = test;
}
So in my ZUser Class (Controller) I did the following code:
[Serializable]
public class ZUser : User
{
String connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
public List<ZUser> ListAll()
{
List<ZUser> lstUser = new List<ZUser>();
using (DataContext db = new DataContext(connString))
{
Table<User> Users = db.GetTable<User>();
var query =
from usr in Users
where usr.Name == "Test"
select usr;
foreach (ZUser usr in query)
lstUser.Add(usr);
}
return lstUser;
}
}
And my Model (Class generated by my edmx)
namespace System.Model
{
//[Table]
public partial class User
{
public int Codigo { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
public DateTime Created { get; set; }
public DateTime LastLogin { get; set; }
}
}
Problems
If I don't let the [Table] in my Model class (I added that) I got this error. I'm not sure if this is the right way to correct it.
The type '{0}' is not mapped as a Table.
After "fixing" the problem from above. I got this new one in my foreach (ZUser usr in query).
The member '{0}.{1}' has no supported translation to SQL.
I don't know how to fix or create a workaround this one.
Amazing, this feature of linq!
Really intresting!
After some searches on msdn and test it in application,
maybe you miss the Column attribute over all single class members:
[Column(CanBeNull = false, DbType = "int")]
And maybe you must uncomment the Table attribute on top of User declaration
Hope this help!
I'm using the mongo-csharp-driver to query my Mongo entities.
I have the following objects which are stored in the Mongo:
public class Table
{
public int Id { get; private set; }
public string Description{ get; private set; }
public List<Player> Players { get; private set; }
public Table()
{
}
}
public class Player
{
public int Id { get; private set; }
public string Username{ get; private set; }
public Player()
{
}
}
When I'm trying to query the "Table" object by id or description, I get the appropriate results, but when I try to query by the list of player, I get null:
// Works ok
var tab1 = mongo.GetCollection<Table>().Where(g => g.Description == "Test");
// Always return null, although should return the same result
var tab2 = mongo.GetCollection<Table>().Where(g => g.Players.Count > 90).FirstOrDefault();
What am I missing here?
Thanks,
Nir.
The issue is that Count property is translated into the $size query operator.
From the linked Advanced Queries page you can see that:
"You cannot use $size to find a range of sizes (for example: arrays
with more than 1 element)."