Returning a String from a SQL query in MVC Entity Framework 6 Controller - sql

I want to execute a basic SQL query (e.g. "SELECT string FROM table WHERE criteria = TRUE) on MVC Entity Framework 6. The returned type is to be a string. I have tried many possible things but they return the SQL query string or something completely irrelevant, such as:
SELECT [Extent1].[Fname] AS [Fname]
FROM [dbo].[Developer] AS [Extent1]
WHERE ([Extent1].[Login] = #p__linq__0)
OR (([Extent1].[Login] IS NULL) AND (#p__linq__0 IS NULL))
I have the following in my controller:
var data = db.Developers.Where(y => y.Login.Equals(username)).ToList();
This is my Developers class:
public partial class Developer
{
public int PersonID { get; set; }
public string Fname
{
get { return Fname.ToString(); }
set { Fname = value;}
}
public string Lname { get; set; }
public string Developer1 { get; set; }
public string Login { get; set; }
public Nullable<bool> IsManager { get; set; }
public Nullable<bool> IsPF { get; set; }
public Nullable<bool> IsLW { get; set; }
public string Prov { get; set; }
}
I would like to get the string that this query returns.
Somehow this works when this is made to a List and passed to the View model, where it is displayed using #Html.DisplayFor().
Is there a way to execute my SQL command in my controller and get a string value in return?

FirstOrDefault() to return the first element, in this case there is only one.
You should include a validation if username doesnt exist.
Developer myDeveloper = db.Developers
.Where(y => y.Login.Equals(username))
.ToList()
.FirstOrDefault();
return myDeveloper.Lname;

Related

Why it produces additional SQL queries when use AutoMapper even though all data have been retrieved from database?

I'm using LINQ and AutoMapper in my ASP.NET web project to retrieve data from database. Here is my controller action to retrieve and list all data from database:
public ActionResult ListPlans()
{
db.Database.Log = (log) => System.Diagnostics.Debug.WriteLine(log);
var listPlan = db.Plans?.OrderByDescending(p => p.PlanId).ToList();
var listPlanViewModels = Mapper.Map<IEnumerable<PlanViewModel>>(listPlan);
return View(listPlanViewModels);
}
When I debug it step by step with Visual Studio, I noticed all data from database have been retrieved and put into memory after this line below was executed:
var listPlan = db.Plans?.OrderByDescending(p => p.PlanId).ToList();
Here is the SQL query produced by above line:
SELECT
[Extent1].[PlanId] AS [PlanId],
[Extent1].[PlanName] AS [PlanName],
[Extent1].[PlanStatus] AS [PlanStatus],
[Extent1].[TemplateId] AS [TemplateId],
FROM
[dbo].[Plan] AS [Extent1]
ORDER BY
[Extent1].[PlanId] DESC
But I noticed it produces additional SQL queries to database when executed this line below:
var listPlanViewModels = Mapper.Map<IEnumerable<PlanViewModel>>(listPlan);
It produces SQL query for each row of data in database, the SQL query shows below:
SELECT
[Extent1].[TemplateId] AS [TemplateId],
[Extent1].[Name] AS [Name],
[Extent1].[Description] AS [Description],
[Extent1].[IsActive] AS [IsActive],
FROM
[dbo].[Template] AS [Extent1]
WHERE
[Extent1].[TemplateId] = #EntityKeyValue1
My question is: why does it produce additional SQL queries even though all data in my database table have been retrieved and put into memory?
How to avoid these additional SQL query when using AutoMapper? It caused performance issue because of these additional queries.
Thanks in advance.
Here are my model and view model:
public partial class Plan
{
public int PlanId { get; set; }
public string PlanName { get; set; }
public string PlanStatus { get; set; }
public System.Guid TemplateId { get; set; }
public virtual Template Template { get; set; }
}
public class PlanViewModel
{
[DisplayName("Plan ID")]
public int PlanId { get; set; }
[DisplayName("Name")]
public string PlanName { get; set; }
public Guid TemplateId { get; set; }
[DisplayName("Template")]
public string TemplateName { get; set; }
[DisplayName("Status")]
public string PlanStatus { get; set; }
}

Handling queries over related documents in RavenDB

I have a project where I have a set of forms:
public class Form
{
public string Id { get; set; }
public string Name { get; set; }
public IList<string> FieldValueIds { get; set; }
public string UserId { get; set; } // the user who completed the form.
public string FormTemplateId { get; set; }
}
Which each "implement" a form template selected at creation of the form.
public class FormTemplate
{
public string Id { get; set; }
public string Name { get; set; }
public IList<string> FieldIds { get; set; }
}
Which defines which fields are present within the form. Each field
public class FormField
{
public string Id { get; set; }
public string Name { get; set; }
public string Caption { get; set; }
public ValueType DataType { get; set; } // Enum specifying the type of data this field accepts.
}
Stores information about the field such as a description and what type it is expecting. Each FormField can be present in multiple FormTemplates with the values for the form being stored as FieldValue objects related to the Form itself.
public class FieldValue
{
public string Id { get; set; }
public string FieldId { get; set; }
public string ValueAsJsonString { get; set; }
}
Other objects include the User Object:
public class User
{
public string Id { get; set; }
public string Username { get; set; }
public string GivenNames { get; set; }
public string Surname { get; set; }
}
I would like to be able to perform a query to find all Forms completed by a user with a specified name, or all Forms where a field with name X has value Y and so forth.
I have looked into usage of indexes as specified in the documentation Indexing related documents, however the implementation as presented in the documentation threw a NotSupportedException when I implemented the example as follows:
class FormTemplates_ByFieldAndName : AbstractIndexCreationTask<FormTemplate>
{
public class Result
{
public string Name { get; set; }
public IList<string> FieldNames { get; set; }
}
public FormTemplates_ByFieldAndName()
{
Map = FormTemplates => from FormTemplate in FormTemplates
select new
{
Name = FormTemplate.Name,
FieldNames = FormTemplate.FieldIds.Select(x => LoadDocument<FormField>(x).Name)
};
}
}
// in code:
IList<FormTemplate> TestResults = session.Query<FormTemplates_ByFieldAndName.Result, FormTemplates_ByFieldAndName>()
.Where(x => x.Name == "TemplateName" || x.FieldNames.Contains("FieldName"))
.OfType<FormTemplate>()
.ToList();
As best as I can tell this was implemented correctly, however I have seen a suggestion to replace the .Contains with a .Any implementation instead. In lieu of this I have been experimenting with a different approach by applying successive .Where arguments. Like so:
var pre = session.Query<FormTemplates_ByFieldAndName.Result, FormTemplates_ByFieldAndName>();
var pr2 = pre.Where(x => x.Name == "TypeTest25");
List<FormTemplate> TestResults = pr2
.Where(x => x.FieldNames.Any(a => a == "field25"))
.OfType<FormTemplate>()
.OrderByScoreDescending()
.ToList();
Modifying the system to perform in a more factory oriented approach by applying successive filters based on a supplied string in a pre-specified format.
Is this the way I should be going for this implementation and if not what should I be changing? In particular if I am to proceed with the Indexing option how would I apply this technique to the nested relationship between Forms and FormFields through FormTemplates.
You seems to be trying to do this in a way that is mostly relational, but you don't have to.
Instead of trying to have a set of independent documents that each has part of the data, just store it all in a single document.
public class Form
{
public string Id { get; set; }
public string Name { get; set; }
public IList<FieldValue> FieldValues { get; set; }
public string UserId { get; set; } // the user who completed the form.
public string FormTemplateId { get; set; }
}
public class FieldValue
{
public string Id { get; set; }
// can store the value directly!
//public string ValueAsJsonString { get; set; }
public object Value {get; set; }
}
This will generate documents that looks like this:
{
"Id": "forms/1234",
"Name": "Tom",
"FieldValues": [
{
"Id": "FromValues/SchoolDistrictName",
"Value": "ABi195"
}
],
"UserId": "users/tom",
"FormTemplateId": "FromTemplate/1234"
}
Which is a much more natural way to model things.
At that point, you can use RavenDB's ability to index dynamic data, see the docs here:
https://ravendb.net/docs/article-page/3.5/Csharp/indexes/using-dynamic-fields

Linq not seeing properties

I have the following class and I'm trying to access it's properties from a different related class as follows:
var nuInfo = recipe.RECIPE_INGREDIENT
.Select(i => i.INGREDIENT.INGREDIENT_NUTRITIONAL_INFO)
.Where(ni => ni.NUTRITIONAL_INFO.Main == 1);
However, I can't access any (virtual or not) properties of INGREDIENT_NUTRITIONAL_INFO.
The INGREDIENT_NUTRITIONAL_INFO class is as follows:
public class INGREDIENT_NUTRITIONAL_INFO
{
public int IngredientId { get; set; }
public int Nutritional_InfoId { get; set; }
public double Amount { get; set; }
public DateTime DateSubmitted { get; set; }
public DateTime DateModified { get; set; }
public string SubmittedBy { get; set; }
public string ModifiedBy { get; set; }
public virtual AspNetUsers AspNetUsers { get; set; }
public virtual AspNetUsers AspNetUsers1 { get; set; }
public virtual INGREDIENT INGREDIENT { get; set; }
public virtual NUTRITIONAL_INFO NUTRITIONAL_INFO { get; set; }
}
Error Code is as follows:
CS1061 'ICollection' does not contain a definition for 'NUTRITIONAL_INFO' and no extension method 'NUTRITIONAL_INFO' accepting a first argument of type 'ICollection' could be found (are you missing a using directive or an assembly reference?)
Am I missing something in Linq? Am I trying to traverse across too many relationships?
The error is because you're not adding .First() or .FirstOrDefault() on the end of your query:
var nuInfo = recipe.RECIPE_INGREDIENT.Select(i => i.INGREDIENT.INGREDIENT_NUTRITIONAL_INFO)
.Where(ni => ni.NUTRITIONAL_INFO.Main == 1)
Your code is trying to access NUTRITIONAL_INFO as a property of the collection, not as the property of a member in the collection.
The problem is that nuInfo is not just one INGREDIENT_NUTRITIONAL_INFO object, your LINQ query returns a result as a IEnumerable<T>.
If you want to get a single result you can use First(),FirstOrDeafult, Single() or SingleOrDefault to instead return a single result.
var nuInfo = recipe.RECIPE_INGREDIENT
.Select(i => i.INGREDIENT.INGREDIENT_NUTRITIONAL_INFO)
.FirstOrDefault(ni => ni.NUTRITIONAL_INFO.Main == 1);

How to bind custom model class in mvc

I am new in MVC. I am working on a project where i have created a model class and also context class which is working good if i view the record in normal view.
but if i try to get the data in group by "Series_Name" and bind it into same model class it gives error. here is my code
Here is Model class and DBContextClass
[Table("tblvideo")]
public class TVSerial
{
[Key]
public Int64 Video_ID { get; set; }
public string Series_Name { get; set; }
public string Season_No { get; set; }
public string Episode_No { get; set; }
public string Episode_Name { get; set; }
public string Time_Duration { get; set; }
public string File_Url_480p { get; set; }
public string File_Url_720p { get; set; }
public string Description { get; set; }
public bool Is_Active { get; set; }
public string Image_Url_Small { get; set; }
public string Image_Url_Big { get; set; }
}
public class TvSerialContext : DbContext
{
public DbSet<TVSerial> TvSerials { get; set; }
}
Here is controller class:
public class TvSerialController : Controller
{
public ActionResult ListAllTvSerial()
{
try
{
TvSerialContext tvContext = new TvSerialContext();
List<TVSerial> tv = tvContext.TvSerials.ToList();
return View(tv);
}
catch (Exception ex)
{
return Content(ex.Message);
}
}
}
Above code works as expected, but if i am doing this :
public ActionResult ListAllSeason(string serial)
{
try
{
TvSerialContext tvContext = new TvSerialContext();
List<TVSerial> tv = tvContext.TvSerials.Where(tvs => tvs.Series_Name == serial).Distinct().ToList();
return View(tv);
}
catch (Exception ex)
{
return Content(ex.Message);
}
}
it return all rows , i just want single row from every series_name and custom field "Series_Name,Season_No,Image_Url_Big"
i don't know how to achieve this.
getting result :
Expected result:-
You could do this by creating a view model and using a .GroupBy() clause
public class TVSerialVM
{
public string SeriesName { get; set; }
public string SeasonNo { get; set; }
public string ImageUrl { get; set; }
}
and the query to project into your view model
List<TVSerialVM> model = tvContext.TvSerials.Where(t => t.Series_Name == serial)
.GroupBy(t => new { t.Series_Name, t.Season_No, t.Image_Url_Big })
.Select(t => new TVSerialVM
{
SeriesName = t.Key.Series_Name,
SeasonNo = t.Key.Season_No,
ImageUrl = t.Key.Image_Url_Big
}).ToList();
Side note: Your duplicating data in the database (the season number and the image url). You should consider moving the image urls to another table with a relationship to the season number.
The reason you are getting multiple values even though you are using distinct is the Distinct method does not know what "equal" is for TVSerial.
You can use Distinct with IEqualityComparer.
https://msdn.microsoft.com/en-us/library/vstudio/bb338049(v=vs.100).aspx
Distinct is not guaranteed to on custom objects it doesn't know what to compare. I have used this SO in the past to make my custom object work with Distinct.
Creating a distinct list of custom type in C#

RavenDB TransformResults

I'm trying to use the TransformResults feature, and I can't get it to work. I'm not totally sure I understand this feature, perhaps there is another way to solve this problem. What I want is just the Id from the Order and the email addesses from the Customer and the Entrepreneur. I am happy for all tips that can take me in the right direction. Here is my code.
Document
public class OrderDocument
public string Id {get; set }
public EntrepreneurInfo EntrepreneurInfo { get; set; }
public CustomerInfo CustomerInfo { get; set; }
public OrderStatus CurrentOrderStatus { get; set; }
}
Info classes
public class EntrepreneurInfo
{
public string EntrepreneurDocumentId { get; set; }
public string Number { get; set; }
public string Name { get; set; }
}
public class CustomerInfo
{
public string CustomerDocumentId { get; set; }
public string Number { get; set; }
public string Name { get; set; }
}
The info classes are just subsets of a Customer and Entrepreneur documents respectively.
The Customer and Entrepreneur documents inherits from a base class ( AbstractOrganizationDocument) that has the EmailAddress property.
My Index
public class OrdersApprovedBroadcastingData :
AbstractIndexCreationTask<OrderDocument, OrdersApprovedBroadcastingData.ReduceResult>
{
public OrdersApprovedBroadcastingData()
{
this.Map = docs => from d in docs
where d.CurrentOrderStatus == OrderStatus.Approved
select new
{
Id = d.Id,
CustomerId = d.CustomerInfo.CustomerDocumentId,
EntrepreneurId = d.EntrepreneurInfo.EntrepreneurDocumentId
};
this.TransformResults = (db, orders) => from o in orders
let customer = db.Load<CustomerDocument>(o.CustomerId)
let entrepreneur = db.Load<EntrepreneurDocument>(o.EntrepreneurId)
select
new
{
o.Id,
o.CustomerId,
CustomerEmail = customer.EmailAddress,
o.EntrepreneurId,
EntrepreneurEmail = entrepreneur.EmailAddress
};
}
public class ReduceResult
{
public string Id { get; set; }
public string CustomerId { get; set; }
public string CustomerEmail { get; set; }
public string EntrepreneurId { get; set; }
public string EntrepreneurEmail { get; set; }
}
}
If I look at the result of this Index in Raven Studio I get null values for all fields except the Id. And finally here is my query.
Query
var items =
this.documentSession.Query<OrdersApprovedBroadcastingData.ReduceResult, OrdersApprovedBroadcastingData>()
.Select(x => new OrdersToBroadcastListItem
{
Id = x.Id,
CustomerEmailAddress = x.CustomerEmail,
EntrepreneurEmailAddress = x.EntrepreneurEmail
}).ToList();
Change your index to:
public class OrdersApprovedBroadcastingData : AbstractIndexCreationTask<OrderDocument>
{
public OrdersApprovedBroadcastingData()
{
Map = docs => from d in docs
where d.CurrentOrderStatus == OrderStatus.Approved
select new
{
};
TransformResults = (db, orders) =>
from o in orders
let customer = db.Load<CustomerDocument>(o.CustomerInfo.CustomerDocumentId)
let entrepreneur = db.Load<EntrepreneurDocument>(o.EntrepreneurInfo.EntrepreneurDocumentId)
select new
{
o.Id,
CustomerEmailAddress = customer.EmailAddress,
EntrepreneurEmailAddress = entrepreneur.EmailAddress
};
}
}
Your result class can simply be the final form of the projection, you don't need the intermediate step:
public class Result
{
public string Id { get; set; }
public string CustomerEmailAddress { get; set; }
public string EntrepreneurEmailAddress { get; set; }
}
You don't have to nest this class in the index if you don't want to. It doesn't matter either way. You can query either with:
var items = session.Query<Result, OrdersApprovedBroadcastingData>();
Or with
var items = session.Query<OrderDocument, OrdersApprovedBroadcastingData>().As<Result>();
Though, with the first way, the convention tends to be to nest the result class, so really it would be
var items = session.Query<OrderDocument.Result, OrdersApprovedBroadcastingData>();
Note in the index map, I am not including any properties at all. None are required for what you asked. However, if you want to add a Where or OrderBy clause to your query, any fields you might want to filter or sort on should be put in there.
One last thing - the convention you're using of OrderDocument, CustomerDocument, EntrepreneurDocument, is a bit strange. The usual convention is just Order, Customer, Entrepreneur. Think of your documents as the persisted form of the entities themselves. The convention you are using will work, it's just not the one usually used.