Entity framework: Retrieving data with column name and value - c#

If I use:
private List<string> GetDataFrom()
{
var result = new List<string>();
using (var context = new mainEntities())
{
var matches = context.data.Where(s => s.Width == 500).ToList();
result.AddRange(matches.Select(t => t.Key));
}
return result;
}
It is giving me perfect results, but I want to use a method where I can use column name and value, like this:
private List<string> GetDataFrom(string columnName, int valToMatch)
{
var result = new List<string>();
using (var context = new mainEntities())
{
var propertyInfo = typeof(data).GetProperty(columnName).Name;
var matches = context.data
.Where(p => p.propertyInfo == valToMatch);
result.AddRange(matches.Select(t => t.Key));
}
return result;
}
This Method obviously doesn't work, so how can I do the same?
I am using SqlLite, so some answers given do not apply.
The whole problem is using propertyInfo the wrong way.
I tried various different approaches but no success.
This question is not a duplicate, because the suggested questions and their answers do not help much.
I like this question to be reopened.
I have found an answer myself I like to share.

ToString() method can not be translated into relevant SQL.
Try to use SqlFunctions.StringConvert(valToMatch) instead of valToMatch.ToString()

.ToString()
Doesn't works inside the Linq Query.
Inststed of .ToString() Function Use the following
SqlFunctions.StringConvert()
SEE THE FOLLOWING ANSWER
https://stackoverflow.com/a/3292773/3736442

Related

Mongodb:Fail to get GridFSFileInfo by ObjectID, but succeed by filename

I fail to get GridFSFileInfo by ObjectID, but succeed by filename,
and the error message is:
Unable to determine the serialization information for x=>x.Id
string objectID = ObjectIDTxt.Text.Trim();
GridFSBucketOptions bucketOptions = new GridFSBucketOptions();
bucketOptions.BucketName = "myBucket";
ObjectId gridfsObjectID = new ObjectId(objectID);
//by filename will succeed
//var filter = Builders<GridFSFileInfo>.Filter.Eq(x => x.Filename, "myfilename.pdf");
//by ObjectID will fail
var filter = Builders<GridFSFileInfo>.Filter.Eq(x=>x.Id,gridfsObjectID);
var findOptions = new GridFSFindOptions();
findOptions.Limit = 1;
var myBucket = new GridFSBucket(_database, bucketOptions);
using (var taskOfCursor = Task.Run(() => myBucket.FindAsync(filter, findOptions)))
{
var taskOfList = Task.Run(() => taskOfCursor.Result.ToListAsync());
GridFSFileInfo fileInfo = taskOfList.Result.FirstOrDefault();
if (fileInfo != null)
{
FileNameLbl.Text = fileInfo.Filename;
}
}
I'm using Mongodb 3.0,c# driver 2.1,wird tiger storage engine.
Forgive me about the use of many 'Task.Run()',because for some reason I need to sync call async mongo methods.
Any suggestions will be appreciated...
thx
Unable to determine the serialization information for x=>x.Id
As the error suggests, you can't use x.Id inside your query in this way. The lambda expression provided is used to retrieve the name of the property and it doesn't understand what x.Id is.
You may try this:
var filter = Builders<GridFSFileInfo>.Filter.Eq("_id", gridfsObjectID);
which uses this overload of the Eq method and performs the implicit conversion from String to FieldDefinition.
Expressions seem a bit puzzling for me as well, but you may find more information related to Expression in the answers to this question: Why would you use Expression> rather than Func?
You can add the lambda syntax directly in Find method:
myBucket.FindAsync(x => x.Id == new MongoDB.Bson.ObjectId(objectID), findOptions)

Formatting Select Statement Using Dynamic Linq

I've been looking into this for quite some time now and cannot figure out a resolution. I Originally Tried formatting A dynamic linq Statement as you can see here in this post
I declared a class:
public class DynamicHelper
{
public string FormattedLink(string DisplayText, string ID)
{
return "" + DisplayText + "";
}
public string FormattedLink(string DisplayText, int ID)
{
return "" + DisplayText + "";
}
}
After I inserted a new type in DynamicLinq into the predefinedTypes
,typeof(DynamicHelper) //around line 635
I have a program which is attempting to Invoke the FormattedLink inside of a dynamic linq select:
using (var Model = new MK3Entities())
{
DynamicHelper Dh = new DynamicHelper();
var TOrigin = (Model.Titles.Where("ID > 19632")
.Select("new(ID, #0.FormattedLink(ExtTitleID, ID) as ExtTitleID )", Dh) as System.Collections.IEnumerable)
.Cast<dynamic>().Take(10).ToList();
Console.ReadKey();
}
When I execute this program I get a runtime exception "LINQ to Entities does not recognize the method 'System.String FormattedLink(System.String, Int32)' method, and this method cannot be translated into a store expression."
Any Ideas on how to fix this... I just need simple formatting from Dynamic Select.
The error message is pretty self explanatory. The database doesn't know how to translate that method into SQL. You need to fetch the information that the method needs in your database query and then call that function on the results, rather than in the query.
I'm not sure why you need it to be dynamic, it seems the solution you present is very overly complicated. I would write it as:
using (var Model = new MK3Entities())
{
DynamicHelper Dh = new DynamicHelper();
var TOrigin = Model.Titles
.Where("ID > 19632")
.Select(t => new { ID = t.ID, ExtTitleID = t.ExtTitleId })
.Take(10)
.ToList() // Execute SQL Statement
.Select(t => new {ID = t.ID, Link = nh.FormattedLink(ExtTitleID, ID)})
.ToList();
Console.ReadKey();
}
I'm returning an List<anonymous'1> object instead of a dynamic object (because I've never had the need for dynamic objects) so you can adjust it accordingly.
I just solved similiar problem few hours back.
YOu need ToList() that works with Dynamic linq. Check out this thread: Can't find property or field on a dynamic object
Just copy paste those to your project, and later:
var TOrigin = (Model.Titles.Where("ID > 19632")
.ToAnonymousList()
.Select("new(ID, #0.FormattedLink(ExtTitleID, ID) as
ExtTitleID )", Dh) as System.Collections.IEnumerable);

How to determine database table field and get the value with Entity Framework

I'm looking for a method to getdatabase table's field with variable thing.
I wrote a stupid and unworking method to explain what I need:
using (var dbContext = new db_ReadyEngine_MSSQL())
{
string nameOfField = "UserName";
var table = dbContext.tbl_User;
foreach (var x in table)
{
string fieldValue = x.nameOfField;
}
}
Here, I'm trying to determining column name which it nameOfField...
You may call data from DataTable by using name of column, as example:
Object o = dataTable.Rows[0][nameOfField];
try this:
List<string>values = new List<string>();
using (var dbContext = new db_ReadyEngine_MSSQL())
{
values = (from s in dbContext.tbl_User select s.Username).ToList();
}
return values
Assuming I am reading your question correctly, you want to get the value of a column, whose name is only known at runtime?
If so, have a look at the code below. It will pull the properties from the object type, search for the one that matches the nameOfField value, and then pull attempt to pull a value from it.
foreach (var x in table)
{
var fieldValue = x.GetType().GetProperties().Where(a => a.Name == nameOfField).Select(p => p.GetValue(x, null)).FirstOrDefault();
}
U can use Reflection to get value of Property using its String Name
using (var dbContext = new db_ReadyEngine_MSSQL())
{
string nameOfField = "UserName";
var table = dbContext.tbl_User;
foreach (var x in table)
{
string fieldValue = typeof(x).GetProperty(nameOfField ).GetValue(x, null) as string;
}
}
You can use Entity SQL for this without typing the query itself:
IEnumerable<object> GetFieldValues<T>(DbContext context, string fieldName)
where T : class
{
var oc = ((IObjectContextAdapter)context).ObjectContext;
ObjectQuery<T> q = oc.CreateObjectSet<T>();
return q.Select("it." + fieldName)
.AsEnumerable()
.Select(x => x[0]);
}
The trick is that an ObjectSet (the predecessor, sort of, or DbSet) can easily be cast to an ObjectQuery, which is the base of Entity SQL. By default, the command text uses "it" as alias for the table in the query, so if you want the value of one specific field, you must prefix it by the alias, and there you go.
The Select returns a DbDataRecord. The first value from this record is returned.
The advantage of this method over others is that it only queries the requested field from the database.
Of course, if you know the type of the field in question up front, you can make a strong-typed version of this method.

Linq To SQL Method with .Contains fails

I have the below Linq To SQL Method. When I step through the code spcCodeIDs contains the seven entries I am expecting. However I get a run-time exception of
Method 'Boolean Contains(System.String)' has no supported translation to SQL.
What am I missing?
public static DataTable GetSPCCodeList()
{
using (var context = ProviderDataContext.Create())
{
IQueryable<tblProviderAdminSPCCode> tSPCCode = context.GetTable<tblProviderAdminSPCCode>();
IList<string> spcCodeIDs = BLLCmo.FCApprovedSPCsForGreenSheet();
return (tSPCCode
.Where(spcCode => spcCode.Inactive == null && spcCodeIDs.Contains(spcCode.SPCCodeID))
.OrderBy(spcCode => spcCode.SPCCodeID)
.Select(spcCode => new { spcCode.SPCCodeID, spcCode.SPCDescription, spcCode.SPCCategoryID }))
.CopyLinqToDataTable();
}
}
LINQ to SQL can only support Contains translations form a concrete list and not the IList interface.. try changing your line from
IList<string> spcCodeIDs = BLLCmo.FCApprovedSPCsForGreenSheet();
to
List<string> spcCodeIDs = BLLCmo.FCApprovedSPCsForGreenSheet().ToList();
You need to pass a string as a parameter to Contains. So trying passing spcCode.SPCCodeID.ToString()

Linq-to-sql logic pain

I've been trying to get the following method cleaned up using more sensible and lean syntax, but I'm striking serious headaches when it comes to aggregate clauses and filtering using L2S. Particularly, I feel I should be able to use a .Contains() method to filter out objects whose tags fit the string parameter passed in the method, but it hasn't worked.
public TagListViewModel GetTagModel(string Name)
{
var model = new TagListViewModel();
var repo = new SimpleRepository("Wishlist");
var ideas = repo.All<Idea>();
List<Idea> ideaList = new List<Idea>();
foreach (Idea i in ideas)
{
var query = from tag in repo.All<Tag>()
join ideatag in repo.All<IdeaTag>()
on tag.ID equals ideatag.TagId
where ideatag.IdeaId == i.ID
select tag;
i.Tags = query.ToList<Tag>();
ideaList.Add(i);
}
foreach (Idea i in ideaList)
{
var query = from vote in repo.All<IdeaVotes>()
where vote.IdeaId == i.ID
select vote;
i.Votes = query.ToList<IdeaVotes>();
}
// Here begins the problem area. I should be able to get a tag from the repo
// whose name matches the "Name" parameter and then call a .Contains() method to
// filter this list, shouldn't I?
List<Idea> filteredTagList = new List<Idea>();
foreach (Idea item in ideaList){
foreach(Tag t in item.Tags)
{
if (t.Name == Name)
filteredTagList.Add(item);
}
}
model.Ideas = filteredTagList;
return model;
}
It's ugly. I know it's ugly but after over 2 hours of playing with several preferred variations I still can't get it to filter the way it's supposed to. Where am I going wrong?
This should be equivalent assuming there are no duplicate tags on a single Idea.
model.Ideas = ideaList.Where(
idea => idea.Tags.Any(
tag => tag.Name == Name)).ToList();

Categories

Resources