Get multiple data from tables Using Entity Framework - c#

public ActionResult Hotel_Read(string text)
{
var result = GetHotel().Where(c => c.Name.Contains(text) || c.City.Contains(text) || c.Country.Contains(text)).ToList();
return Json(result, JsonRequestBehavior.AllowGet);
}
private static IEnumerable<HotelViewModel> GetHotel()
{
using (TravelAgancyEntities1 db = new TravelAgancyEntities1())
{
var query = db.Hotels
.Include(p => p.City.Country).Distinct().ToList();
return query.Select(Hotel => new HotelViewModel
{
Name = Hotel.Name,
City = Hotel.City.City_Name,
**Line 10-** Country = Hotel.City.Country.Country_Name,//!!!
});
}
}
When I run the code without line 10, it is working successfully, but when that code is run with line 10, then it's not working.

I assume your code should run properly. the only thing that makes me suspicious, is that you are trying to retrieve all Hotel table data plus 2 other table(with include)
try this :
var q = (from x in db.Hotels.Include(c => c.City).Include(c => c.City.Country)
where x.Id == 5030
select x).Distinct().ToList();
string s = q[0].City.Country.Country_Name;
Limit your select with Where clause.

You don't need Includes here because there is no application logic involved in creation of HotelViewModel instances.
Simple query:
db.Hotels.Select(h => new HotelViewModel
{
Name = h.Name,
City = h.City.City_Name,
Country = h.City.Country.Country_Name,
}).ToList();
Will return from the DB exactly the data that you need.
When you first do Includes, and then call ToList();:
var query = db.Hotels.Include(p => p.City.Select(x => x.Country))
.Distinct()
.ToList();
You fetch form DB all the Hotel properties, all the City properties and
all the Country properties while all you really need is only their names.

Related

How Linq query fetch data from multiple tables without using join

I have 3 tables in my database which have no relation with each other. what i want is to implement the search operation on my website. So that when a word is submit in search box the query go through all the tables and fetch the data wherever it find that word. I can fetch the data from single table.
public ActionResult Searchresult(string searchString)
{
var article = (from c in db.Tbl_Article select c );
article = article.Where(s => s.Article_Title.Contains(searchString));
var blog = (from c in db.Tbl_Blog select c );
blog = blog.Where(s => s.Blog_Title.Contains(searchString));
var history = (from c in db.Tbl_History select c);
history = history.Where(s => s.Title.Contains(searchString));
var result = article.Select(x => x.Article_Title).Union(blog.Select(x => x.Blog_Title)).Union(history.Select(x => x.Title)).ToList();
// ViewBag.result = result.ToString();
return View(result);
}
Please don't confuse about what i return in action method, its a very long and unnecessary code for that question.
By using this code I successfully get the search result from one table.
Now I want same result from all the tables present in the database. that's where I'm stuck. I searched so many article for that but didn't find any solution that's in the last I myself asking this.
Thanks
This may work
Public ActionResult Index(string searchString)
{
var query1 = (from c in db.TableArticle select c);
if (!String.IsNullOrEmpty(searchString))
{
query1 = query1.Where(s =>
s.Article_Title.Contains(searchString)
|| s.Article_Description.Contains(searchString) ||
s.Written_By.Contains(searchString) ||
s.Organisation.Contains(searchString)
||s.Source.Contains(searchString));
}
var query2 = (.......);
var query3 = (.......);
var finalResult = query1.Select(x => x.columnName).Union(query2.Select(x => x.columnName)).Union(query3.Select(x => x.columnName));
// OR
var finalResult = query1.Select(x => x.columnName).Concat(query2.Select(x => x.columnName)).Concat(query3.Select(x => x.columnName));
}
I have just implemented and checked in my local same query, and it's working fine,
The given answer is right by #Khairul Alam,
I have just optimized the code and run the same thing in my local its working fine
var searchString = "School";
if (!String.IsNullOrEmpty(searchString))
{
var query1 = _context.Jobs.Where(s =>
s.JobTitle.Contains(searchString)
|| s.LocationDescription.Contains(searchString));
var query2 = _context.Recruiters.Where(s =>
s.RecruiterName.Contains(searchString));
var query3 = _context.Sectors.Where(s =>
s.SectorName.Contains(searchString));
try
{
var finalResult = query1.Select(x => x.JobTitle).Union(query2.Select(x => x.RecruiterName)).Union(query3.Select(x => x.SectorName)).ToList();
}
catch (Exception)
{
throw;
}
contain image which output of final result
If you still getting an issue , please share some code or where exactly you getting the error , so we can solve them
hope this will be helpful to you
Thanks

selecting properties from other table with Lambda expression

I am less experienced with Lambda expression for .NET and trying to get data from SQL using lambda expression. With below query, I am able to get data back, but do not want to use include to get all properties from other tables.
public IEnumerable<ResourceGroup> GetAllServersByApplication(string application_name, string environment_name, string status)
{
var query = _context.ResourceGroup
.Include(a => a.Application)
.Include(t => t.Type)
.Include(e => e.ServersGroup).ThenInclude(e => e.Environment)
.Include(s => s.ServersGroup).ThenInclude(s => s.Server)
.Include(s => s.ServersGroup).ThenInclude(s => s.Server).ThenInclude(s => s.Status)
.Where(a => a.Application.Name == application_name && a.ServersGroup.Any(s => s.Environment.Name == environment_name && s.Server.Status.Name == status))
.ToList();
return query;
}
Lets take an example of below include statement.
.Include(s => s.ServersGroup).ThenInclude(s => s.Server)
From s.Server, I only want to select Id,ServerName,Status, and IPAddress. These are the properties from Servers class that I created as a model.
What is the easy way to exclude all the includes and only show properties that I am interested in?
Here are my tables and its properties:
Status table:
Id, Name
Application table:
Id, Name
Servers table:
Id, ServerName, Status
Environments table:
Id, Name
ResourceGroup table:
Id, Name, Application_Id, Environment_Id
ServersResourceGroup table:
Id, Server_Id, Resource_Id
UPDATE 1:
var query = _context.ResourceGroup
.SelectMany(rg => rg.ServersGroup
.Select(sg => new
{
ResourceName = rg.Name,
ApplicationName = rg.Application.Name,
ServerName = sg.Server.ServerName,
EnvironmentName = sg.Environment.Name,
Status = sg.Server.Status.Name
})).Where(a => a.ApplicationName == application_name && a.EnvironmentName == environment_name && a.Status == status).ToList();
return query;
And error from red line on query variable:
UPDATE 2:
Here is the query syntax:
var query = from rg in _context.ResourceGroup
let app = rg.Application
from sg in rg.ServersGroup
let env = sg.Environment
let srv = sg.Server
let stat = srv.Status
where app.Name == application_name
&& rg.ServersGroup.Any(s => s.Environment.Name == environment_name
&& s.Server.Status.Name == status)
select new
{
ResourceGroupName = rg.Name,
ApplicationName = app.Name,
ServerName = srv.ServerName,
Alias = srv.Alias,
IPAddress = srv.IPAddress,
Type = rg.Type.Name,
Status = stat.Name
};
return query;
Here is the red line error I get in query variable:
Your help is really appreciated. :)
Thanks,
Ray
With lambda expressions, you can use SelectMany to flatten 1-n associations into a 1 dimensional list (i.e. parent and child properties side-by-side). In your case, judging from the Where clause, I think only ResourceGroup - ServerGroup is 1 - n, so it should be something like:
var query = _context.ResourceGroup
.SelectMany
(
rg => rg.ServersGroup
.Select(sg => new
{
ResourceGroup = rg.Name,
Application = rg.Application.Name,
Server = sg.Server.ServerName,
// etc.
})
)
Of course it's good to know how to use lambda expressions, but there's really no point in using them when query syntax makes for much better comprehensible code.
The equivalent in query syntax is:
var query = from rg in _context.ResourceGroup
let app = rg.Application
from sg in rg.ServersGroup
let env = sg.Environment
let srv = sg.Server
let stat = srv.Status
where app.Name == application_name
&& sg.ServersGroup.Any(s => s.Environment.Name == environment_name
&& s.Server.Status.Name == status)
select new
{
ResourceGroup = rg.Name,
Application = app.Name,
Server = srv.ServerName,
// etc. use any property from rg, app, sg, srv, stat
};
As you see -
n - 1 associations are represented by a let statement (which really only helps here to shorten the references in the select)
1-n associations are represented by the from ... from syntax, which is query syntax for SelectMany.
I didn't change the Where clause in the query syntax. Maybe you can use ...
where app.Name == application_name
&& env.Name == environment_name
&& stat.Name == status)
... but note that this is different. The original where returns all ResourceGroup having at least one ServerGroup meeting the condition (and maybe other groups with different environments and statuses). The other where only returns data with environments and statuses equal to the search parameters.
Don't Include all the related tables, but Select all the fields you need. You might find it easier to make a new class to hold the data.
Sorry if I can't make a real query statement here, but your question doens't specify the fields you need.

can't Call Other Service in Select Method- Linq [duplicate]

This question already has answers here:
LINQ to Entities does not recognize the method
(5 answers)
Closed 6 years ago.
I have a query to get all Orders with detail . I have a query like this :
return
_orderMasters.OrderByDescending(row => row.Code).Where(row => row.Code == code).Include(row => row.Orderdetails)
.Select(row => new OrderViewModel
{
Code = row.Code,
// other fields
Items = row.Orderdetails.Select(detail => new OrderItemViewModel
{
ProductcombinationId = detail.ProductCombinationId,
ProductId=detail.ProductAttributeCombination.ProductId,
ProductName = detail.ProductCombination.Product.Name,
ProductCombinationName=_productCombinationService.GetCombinationsName(detail.ProductCombinationId,detail.ProductCombination.ProductId) // * this row
}).ToList(),
ShippingAddress = new AddressViewModel()
{
//set fileds value
}
}).FirstOrDefault();
In line that * I need get ProductcombinationName , to do this I call Method in another Service , but getting this Error :
LINQ to Entities does not recognize the method
First Idea Is , add foreach for All rows and call that Method to get ProductcombinationName , But I don't know is it a good way ?
you can not use c# functions into linq because linq try to execute code on sql side where sql doesn't understand what your c# function is and give error.
for this,
you can do like
Items = row.Orderdetails.Select(detail => new OrderItemViewModel
{
ProductcombinationId = detail.ProductCombinationId,
ProductId=detail.ProductAttributeCombination.ProductId,
ProductName = detail.ProductCombination.Product.Name,
ProductCombination = detail.ProductCombination // need to add for next operation
}).ToList(),
and then
foreach(var item in Items)
{
// add ProductCombinationName code here
}
Entity Framework will not run C# code as part of its query, it has to be able to convert the query to an actual SQL expression.
So we will have to restructure your query expression into an expression that Entity Framework can handle.
var orders = _orderMasters.OrderByDescending(row => row.Code)
.Where(row => row.Code == code)
.Include(row => row.Orderdetails)
.ToList();
return orders.Select(row => new OrderViewModel
{
Code = row.Code,
// other fields
Items = row.Orderdetails.Select(detail => new OrderItemViewModel
{
ProductcombinationId = detail.ProductCombinationId,
ProductId=detail.ProductAttributeCombination.ProductId,
ProductName = detail.ProductCombination.Product.Name,
ProductCombinationName=_productCombinationService.GetCombinationsName(detail.ProductCombinationId,detail.ProductCombination.ProductId) // * this row
}).ToList(),
ShippingAddress = new AddressViewModel()
{
//set fileds value
}
}).FirstOrDefault();

How to program sql query with inner join in ASP.Net MVC?

I'm trying to do a LINQ statement using three database tables for my third dropdownlist. Below are my codes but I get an error (for my third dropdownlist) when I choose a cluster in the second dropdownlist.
**//SECTORS**
public JsonResult GetSectors()
{
using (SAMPDBEntities context = new SAMPDBEntities())
{
var ret = context.SECLIBs
.Select(x => new { x.seccd, x.unitacro }).ToList();
return Json(ret, JsonRequestBehavior.AllowGet);
}
}
**//CLUSTERS**
public JsonResult GetCluster(string seccd)
{
using (SAMPDBEntities context = new SAMPDBEntities())
{
var ret = context.CLUSLIBs
.Where(x => x.seccd.Contains(seccd))
.Select(x => new { x.cluscd, x.unitdesc }).ToList();
return Json(ret, JsonRequestBehavior.AllowGet);
}
}
**//EMPLOYEES**
public JsonResult GetEmployee(string cluscd)
{
using (SAMPDBEntities context = new SAMPDBEntities())
{
var ret = context.UNILIBs
.Where(a => a.cluscd.Contains(cluscd))
.Include(x => x.PPSAs.Select(y => y.EMPFILE.empno))
.ToList();
return Json(ret, JsonRequestBehavior.AllowGet);
}
}
Here's my error:
A specified Include path is not valid. The EntityType 'SAMPDBModel.EMPFILE' does not declare a navigation property with the
name 'empno'.
and here's the SQL query (for my third dropdownlist):
SELECT DISTINCT e.empno, e.lname, e.fname, e.mname, c.cluscd
FROM SECLIB a
INNER JOIN CLUSLIB b
ON a.seccd = b.seccd
INNER JOIN UNILIB c
ON b.cluscd = c.cluscd
INNER JOIN PPSA d
ON c.unitcode = d.unitcd
INNER JOIN EMPFILE e
ON d.empno = e.empno
WHERE e.empstat = 1 AND c.cluscd = #cluscd
I need to do a cascading dropdownlist and I need to show the list of employees based on the selected sector and cluster. How can I do that using multiple tables? Please help me. Thanks in advance!
This should be an issue of not specifying the correct respective name that generated from EDMX. Please Can you check the "EMPFILE" Class that generated from Entity Framework It should have similar name with different case sensitive word.
When querying in SQL it does not bother with case sensitivity. But C# is case sensitive language.
And its better if you can post the "EMPFILE" class and database table here.

Join table with object list

I have a table, lets say tblCar with all the related columns like Id, Make, Model, Color etc.
I have a search model for car containing two params Id and Model.
public class CarSearch
{
public int Id { get; set; }
public string Model { get; set; }
}
var carSearchObjets = new List<CarSearch>();
With list of primitive data (like Id list), to get cars with those Ids I could have done:
var idList = new List<int> { 1, 2, 3 };
var carsFromQuery = context.Cars.Where(x => idList.Contains(x.Id);
But if I have to fetch all the cars with Id and model from the list, how do I do it? Simple join cannot be done between in memory objects and tables.
I need something like,
from m in context.Cars
join n in carSearchObjets
on new { Id = n.Id, Model = n.Model } equals new { Id = m.Id, Model = m.Model }
select m;
This obviously won't work.
Please ignore any typos.And if you need more info or the question is not clear, let me know.
One (ugly-but-working) way to manage that is to use concatenation with a "never used" concat char.
I mean a char that should never appear in the datas. This is always dangerous, as... never is never sure, but you've got the idea.
For example, we'll say that our "never used" concat char will be ~
This is not good for perf, but at least working :
var carSearchObjectsConcatenated = carSearchObjets.Select(m => new { m.Id + "~" + m.Model});
then you can use Contains again (concatenating on the db too) : you'll need to use SqlFunctions.StringConvert if you wanna concatenate string and numbers on the db side.
var result = context.Cars.Where(m =>
carSearchObjectsConcatenated.Contains(SqlFunctions.StringConvert((double)m.Id) + "~" + m.Model);
EDIT
Another solution would be to use PredicateBuilder, as mentionned by Sorax, or to build your own Filter method if you don't want a third party lib (but PredicateBuilder is really fine).
Something like that in a static class :
public static IQueryable<Car> FilterCars(this IQueryable<Car> cars, IEnumerable<SearchCar> searchCars)
{
var parameter = Expression.Parameter(typeof (Car), "m");
var idExpression = Expression.Property(parameter, "Id");
var modelExpression = Expression.Property(parameter, "Model");
Expression body = null;
foreach (var search in searchCars)
{
var idConstant = Expression.Constant(search.Id);
var modelConstant = Expression.Constant(search.Model);
Expression innerExpression = Expression.AndAlso(Expression.Equal(idExpression, idConstant), Expression.Equal(modelExpression, modelConstant));
body = body == null
? innerExpression
: Expression.OrElse(body, innerExpression);
}
var lambda = Expression.Lambda<Func<Car, bool>>(body, new[] {parameter});
return cars.Where(lambda);
}
usage
var result = context.Cars.FilterCars(carSearchObjets);
this will generate an sql looking like
select ...
from Car
where
(Id = 1 And Model = "ax") or
(Id = 2 And Model = "az") or
(Id = 3 And Model = "ft")
'PredicateBuilder' might be helpful.
var predicate = PredicateBuilder.False<Car>();
carSearchObjects
.ForEach(a => predicate = predicate.Or(p => p.Id == a.Id && p.Model == a.Model));
var carsFromQuery = context.Cars.AsExpandable().Where(predicate);
Note the text in the link regarding EF:
If you're using Entity Framework, you'll need the complete LINQKit -
for the AsExpandable functionality. You can either reference
LINQKit.dll or copy LINQKit's source code into your application.
Old school solution..
//in case you have a
List<CarSearch> search_list; //already filled
List<Cars> cars_found = new List<Cars>();
foreach(CarSearch carSearch in search_list)
{
List<Cars> carsFromQuery = context.Cars.Where(x => x.Id == carSearch.Id && x.Model == carSearch.Model).ToList();
cars_found.AddRange(carsFromQuery);
}
Abd don't worry about the for loops.
I landed up passing in an xml list as a parameter to the sql query and joined to that:
var xml = new XElement("Cars", yourlist.Select(i => new XElement("Car", new XElement("Id", i.Id), new XElement("Model", i.Model))));
var results = Cars
.FromSql("SELECT cars.*"
+ "FROM #xml.nodes('/Cars/Car') Nodes(Node)"
+ "JOIN Cars cars on cars.Id = Nodes.Node.value('Id[1]', 'int') and cars.Model = Nodes.Node.value('Model[1]', 'varchar(100)')",
new SqlParameter("#xml", new SqlXml(xml.CreateReader())));
For entity-framework-core users I created a nuget package extension:
EntityFrameworkCore.SqlServer.Extensions.Contains

Categories

Resources