I have the following code. It runs fine.
In the place I have marked I'd like to write a query (I assume with LINQ) which extracts the CompanyName where the MainKey == 3028
I suspect this is trivial but I'm new to LINQ and I've looked up some basic LINQ info on MSDN and can't seem to get it to work.
namespace EntityFrameworkExperiment {
class Program {
static void Main(string[] args) {
var models = SelectTop100Models("SELECT top 100 * FROM WH.dbo.vw_DimXXX");
Console.Write("hello world");
//<<<<<<<linq query to pull out companyname when MainKey == 3028
Console.Read();
}
static IEnumerable<MyModel> SelectTop100Models(string myCommandText) {
var connectionString = ConfigurationManager.ConnectionStrings["XXX"].ConnectionString;
using(var conn = new SqlConnection(connectionString))
using(var cmd = conn.CreateCommand()) {
conn.Open();
cmd.CommandText = myCommandText;
using(var reader = cmd.ExecuteReader()) {
while(reader.Read()) {
yield return new MyModel {
MainKey = reader.GetInt32(reader.GetOrdinal("MainKey")),
ServerId = reader.GetInt32(reader.GetOrdinal("ServerId")),
CompanyId = reader.GetInt32(reader.GetOrdinal("CompanyId")),
CompanyName = reader.GetString(reader.GetOrdinal("CompanyName")),
};
}
}
}
}
}
public class MyModel {
public int MainKey { get; set; }
public int ServerId { get; set; }
public int CompanyId { get; set; }
public string CompanyName { get; set; }
}
}
Add using System.Linq
The query should be
var companyName = models
.Where(o => o.MainKey == 3028) // apply the filter
.Select(o => o.CompanyName) // tell it you only need the one property
.FirstOrDefault(); // take the first result it finds or use 'null' if the MainKey does not exist
But there is one thing you have to remember - here you are not using LINQ queries to the SQL server - instead you are retrieving all data in memory and then filtering them in .NET. What this means is that if the database contains millions of rows, they will all be pulled from the SQL server. You are applying TOP 100 but that will get you into trouble if the key 3028 is not within the first 100.
What you should be doing is creating a model using Entity Framework (or a similar tool) and then writing a query that target the classes generated by it. The good thing though is that the LINQ query will be exactly the same - it will just be translated to SQL behind the scenes.
The linq query would be.
var result = from rec in ModelOfWHData.vw_DimCasinos
where (rec.MainKey == 3028)
select rec.CompanyName
The LINQ query below will post-process the IEnumerable you're generating from the T-SQL query, returning a single matching object, or null if not found:
MyModel result = (from m in MyModel
where m.MainKey == 3028
select m).SingleOrDefault();
string companyName = result.CompanyName;
However, I suspect you would be better off using LINQ-to-SQL and actually getting LINQ to generate and execute a T-SQL query for you.
Related
Here is my code the issue I have is the less than comparison in the On clause ... Since Linq doesn't allow this .... Migrating down into the where clause wont work as I am comparing one of the fields to null.
Here is the sql query (THE a.UserID= is hardcoded for now)
SELECT A.Policy, A.Comments, A.EventDTTM, A.Status, A.Reason, A.FollowUp
FROM PP_PolicyActivity A
LEFT JOIN PP_PolicyActivity B
ON(A.Policy = B.Policy AND A.EventDTTM < B.EventDTTM)
WHERE A.UserID = 'Ixxxxxx'
AND B.EventDTTM IS NULL AND a.status = 'open - Pending'
order by A.EventDTTM DESC
I need the result set from the above query as an IEnumerable list to populate a view
I'm tasked with rebuilding an old VB ASP NET that has a set of standing production databases behind it ... i don't have the option of changing the db design. I connecting to the server and database and this query was going against a table on that database.. the model also reflects the layout of the actual table.
The problem is with A.EventDTTM < B.EventDTTM - I can't move this to the where clause as I also have to deal with B.EventDTTM IS NULL in the where clause.
I need to retool the query someway so that it is 'linq' friendly
public class PolicyActivityModel
{
public string Policy { get; set; }
public int PolicyID { get; set; }
public string Status { get; set; }
public string Reason { get; set; }
public string Comments { get; set; }
public DateTime EventDTTM { get; set; }
public string UserID { get; set; }
public DateTime FollowUp { get; set; }
}
Company policy prohibits me from showing the connection string.
I am extremely new to Linq, Any help greatly appreciated
thank you
You can use the navigation property after you get the policy from the database.
var policy = DbContext.First(x => x.Id == 1000);
var otherPolicies = policy.ConnectedPolicies.Where(p => ...);
It's weird being a self-join but this is the most direct translation to Linq:
var query = from leftPP in PP_PolicyActivity
join rightPP in PP_PolicyActivity
on new { Policy = leftPP.Policy, EventDTTM = leftPP.EventDTTM }
equals new { Policy = rightPP.Policy, EventDTTM = rightPP.EventDTTM }
into pp from joinedRecords.DefaultIfEmpty()
where leftPP.UserId == 1
&& leftPP.EventDTTM < rightPP.DTTM)
&& rightPP.EventDTTM == null
&& leftPP.status = "open - Pending"
select new
{
leftPP,
rightPP
}
I free typed this, without models or Intellisense, thus there might be some smaller errors.
You could add the order by in that clause, but it's also still an IQUeryable, so I'd leave it.
And then, to get a List of models:
var results = query.OrderByDescending(x => x.EventDTTM).ToList();
The actual join is lines 2,3,4 and 5. It's verbose and "backwards" from SQL, and most importantly uses anonymous types. Accessing indidual properties will look something like:
results[0].leftPP.PolicyId
I'm trying to select some data from a table in my database using a join in a linq query, but I can't seem to grasp how to save it to the list of DTO's that I would like to return.
I've been looking at this post for directions at using the lambda expression: C# Joins/Where with Linq and Lambda but it seems like that guy is trying to accomplish something slightly different than me; I want to compare the value CPR (from the table Coworkers) and the value CPR (From the table Duties) and select all of those where the Projektname (from the table Duties) are equal to the string projektname.
What I've written so far of the method is this:
public List<CoworkerDTO> GetCoworkers(string projektname)
{
_coworkerlist = new List<CoworkerDTO>();
using (var context = new F17ST2ITS2201608275Entities())
{
var dataset =
from co in context.Coworkers
join du in context.Duties on co.CPR equals du.CPR
where du.Projektname == projektname
select new {Coworkers = co};
foreach (var element in dataset.ToList())
{
_coworkerlist.Add(element);
}
}
return _coworkerlist;
}
The CoworkerDTO looks like this:
class CoWorkerDTO
{
public string Fornavn { get; set; }
public string Efternavn { get; set; }
public int Alder { get; set; }
public string CPR { get; set; }
public decimal AntalTimer { get; set; }
}
The table Coworkers has a column that corresponds to each of the properties above, so I guess my question is how to somehow convert the selection that I get into a list of the CoworkerDTOs.
Sorry for the long post, and if my english is a bit confusing, as it's not my first language.
Thanks in advance :)
You should convert Coworkers entity into CoWorkerDTO. You can do it manually (assume properties have same names and types):
var dtos =
from co in context.Coworkers
join du in context.Duties on co.CPR equals du.CPR
where du.Projektname == projektname
select new CoWorkerDTO {
Fornavn = co.Fornavn,
Efternavn = co.Efternavn,
Alder = co.Alder,
CPR = co.CPR,
AntalTimer = co.AntalTimer
};
return dtos.ToList();
Or you can use something like AutoMapper Queryable Extensions to do that projection automatically:
Mapper.Initialize(cfg =>
cfg.CreateMap<Coworkers, CoWorkerDTO>());
And query with projection will look like
var entities =
from co in context.Coworkers
join du in context.Duties on co.CPR equals du.CPR
where du.Projektname == projektname
select co;
return entities.ProjectTo<CoWorkerDTO>().ToList();
I am having a terrible time trying to get a LINQ statement working.
I have tried using both SQL syntax and lambda following this post:
C# Joins/Where with Linq and Lambda
This is what my working SQL looks like:
SELECT ws_lookup_OccupationGroup.Code
FROM ws_lookup_OccupationGroup
INNER JOIN ws_lookup_Occupation ON
ws_lookup_OccupationGroup.Code = ws_lookup_Occupation.ws_lookup_OccupationGroup_Code
WHERE (ws_lookup_Occupation.Code = N'413')
This is my first attempt and it yields no results:
var query = from occupationGroup in db.ws_lookup_OccupationGroups
join occupations in db.ws_lookup_Occupations on occupationGroup.Code equals occupations.Code
where occupations.Code == model.Client.Client_Details_Enhanced.Occupation.Code
select new
{
OccupationGroup = occupationGroup,
Occupations = occupations
};
Here is my second attempt using Lamdba which also yields no results:
var queryLambda = db.ws_lookup_OccupationGroups
.Join(db.ws_lookup_Occupations,
occupation => occupation.Code,
occupationGroup => occupationGroup.Code,
(occupation, occupationGroup) => new
{
OCCUPATION = occupation,
OCCUPATIONGROUP = occupationGroup
})
.Where(all => all.OCCUPATION.Code == model.Client.Client_Details_Enhanced.Occupation.Code);
I just cannot see what is going wrong...
I don't know is this has any relevance but I am using Code First Entity Framework - he is my model for OccupationGroups & Occupations:
public class ws_lookup_OccupationGroup {
[Key]
[MaxLength(250)]
public string Code { get; set; }
[MaxLength(250)]
public string Name { get; set; }
public int SortOrder { get; set; }
public List<ws_lookup_Occupation> Occupations { get; set; }
}
public class ws_lookup_Occupation {
[Key]
[MaxLength(10)]
public string Code { get; set; }
[MaxLength(250)]
public string Name { get; set; }
[MaxLength(250)]
public string BarbadosMotorFactor { get; set; }
[MaxLength(250)]
public string TrinidadMotorFactor { get; set; }
[MaxLength(250)]
public string OtherRegionsMotorFactor { get; set; }
}
Instead of directly answering your question I will rather come with a suggestion of strategy. One strategy then is to add an extension method that will reveal the SQL your Entity Framework query or IQueryable will run. This can be done in such a manner that you create a unit test and do a Test Driven Development approach or TDD.
You know the SQL you want to get the expected result. It is better then to work with your EF query until you get a SQL that will deliver the result you are after. You can debug an integration test and then work your way towards the end result - the SQL you are after - written in Entity Framework Linq to Entities code.
First off, we can create the following extension method:
public static class IQueryableExtensions
{
/// <summary>
/// Shows the sql the IQueryable query will be generated into and executed on the DbServer
/// </summary>
/// <param name="query">The IQueryable to analyze</param>
/// <param name="decodeParameters">Set to true if this method should try decoding the parameters</param>
/// <remarks>This is the generated SQL query in use for Entity Framework</remarks>
public static string ShowSql(this IQueryable query, bool decodeParameters = false)
{
var objectQuery = (ObjectQuery)query;
string result = ((ObjectQuery)query).ToTraceString();
if (!decodeParameters)
return result;
foreach (var p in objectQuery.Parameters)
{
string valueString = p.Value != null ? p.Value.ToString() : string.Empty;
if (p.ParameterType == typeof(string) || p.ParameterType == typeof(DateTime))
valueString = "'" + valueString + "'";
result = result.Replace("#" +p.Name, p.Value != null ? valueString : string.Empty);
}
return result;
}
}
Then we need some integration test, sample from my own system:
[TestFixture]
public class IqueryableExtensionsTest
{
[Test]
public void QueryableReturnsSqlAndDoesNotThrow()
{
using (var dbContext = ObjectContextManager.ScopedOpPlanDataContext)
{
var operations = from operation in dbContext.Operations
where operation.Status == (int) OperationStatusDataContract.Postponed
&& operation.OperatingDate >= new DateTime(2015, 2, 12)
select operation;
string sql = operations.ShowSql();
Assert.IsNotNull(sql);
}
}
}
While you can of course use Linqpad to find the EF query and SQL you are after for, the benefit of this strategy is that you can use it inside Visual Studio for the more complex real world scenarios, you also get a better debugging experience than switching between VS and Linqpad.
If you debug such an integration test you can then watch the SQL being generated. Note that you also can do Console.WriteLine or Debug.WriteLine to watch the output if you want to run the Integration Test and not debug it.
In your SQL you are joining on the following
ws_lookup_OccupationGroup.Code = ws_lookup_Occupation.ws_lookup_OccupationGroup_Code
But in the Linq you join on
occupationGroup.Code equals occupations.Code
Depending on exactly what your entity looks like I would assume you actually need this
occupationGroup.Code = occupations.ws_lookup_OccupationGroup_Code
Based on your entity it looks like you can do the following with navigation properties instead of joins
var query = from occupationGroup in db.ws_lookup_OccupationGroups
where occupationGroup.Occupations.Any(
o => o.Code == model.Client.Client_Details_Enhanced.Occupation.Code)
select occupationGroup;
To get all the occupation groups that have at least one occupation with the desired code. Or if you just want a combination of group and occupation then you could do
var query = from occupationGroup in db.ws_lookup_OccupationGroups
from occupation in occupationGroup.Occupations
where occupation.Code == model.Client.Client_Details_Enhanced.Occupation.Code
select new
{
occupationGroup,
occupation
};
I am running a query to populate options in a single select drop down menu. When I debug the function below, the query variable contains the resultset that I am expecting. However when I skip next to where it should be returned to, I get the error:
'The entity type or complex type 'Models.zz_Member' cannot be constructed in a LINQ to Entities query."
public IQueryable<zz_Member> GetMembers(string searchText)
{
var _db = new Portal.Models.AuthorizationContext();
IQueryable<zz_Member> query = _db.zz_Members;
return query //.Where(a => a.memLastName.Contains(searchText))
.Select(a => new zz_Member()
{
ID = a.ID,
memFirstName = a.memFirstName,
memLastName = a.memLastName
}
);
}
The zz_Member model object is defined as:
public class zz_Member
{
[ScaffoldColumn(false)]
public int ID { get; set; }
public string memFirstName { get; set; }
public string memLastName { get; set; }
}
The error is thrown when I try to convert to an IList, but when I check the value of memList using the debugger, it shows the error text in the results view.
IQueryable<zz_Member> memList = GetMembers(e.Text);
IList<zz_Member> memList2 = memList.ToList();
I have also tried writing the GetMembers functions to return the list as so:
public IList<zz_Member> GetMembers(string searchText)
{
var _db = new WWCPortal.Models.AuthorizationContext();
return (from m in _db.zz_Members
where m.memLastName.Contains(searchText)
select new zz_Member { ID = m.ID, memFirstName = m.memFirstName, memLastName = m.memLastName }).ToList();
}
Any hints or answers to why the resultset appears to not be getting returned to the caller and put into memList? Thanks.
You cannot use framework dependant/generated entities in projection (with select new), hence the error.
Looks like you are trying to select specific columns instead of all columns, your options are
Project to a new class with those specific members
return all fields/columns for your entities like:
Code:
return query.Where(a => a.memLastName.Contains(searchText)); //without `select new`
When connecting to CRM 2013 is there a smart way to create a lambda expression that gets the entities who's GUID are in a List.
This code breaks on the Where clause and gives the error:
Invalid 'where' condition. An entity member is invoking an invalid property or method.
Code:
private List<UserInformationProxy> GetContactsFromGuidList(List<Guid> contactList)
{
var result = _serviceContext.ContactSet
.Where(x=> contactList.Contains((Guid) x.ContactId)) // this line breaks
.Select(x => new UserInformationProxy()
{
FullName = x.FullName,
Id = x.ContactId
})
.Distinct()
.ToList<UserInformationProxy>();
return result;
}
// return class
public class UserInformationProxy
{
public Guid? Id { get; set; }
public string FullName { get; set; }
public string DomainName { get; set; }
}
Currently I'm solving this by getting all the contacts from the ContactSet and sorting out the ones I want with a loop in my code. This works, but is quite slow as I need to get all 10000 contacts instead of sending the Guids of the 40 Im actually interested in to the SQL server.
QueryExpressions support an In operator, so this should work just fine:
private List<UserInformationProxy> GetContactsFromGuidList(List<Guid> contactList)
{
var qe = new QueryExpression(Contact.EntityLogicalName);
qe.ColumnSet = new ColumnSet("fullname", "contactid")
qe.Criteria.AddCondition("contactid", ConditionOperator.In, list.Cast<Object>().ToArray());
qe.Distinct = true;
var results = service.RetrieveMultiple(qe).Entities.Select (e => e.ToEntity<Contact>()).
Select(x => new UserInformationProxy()
{
FullName = x.FullName,
Id = x.ContactId
});
return results;
}
On a side note, every Contact has to have an Id that is not empty, so there is no need to check for it.
EDIT: It is possible to accomplish using a single query, Daryl posted an answer with the right code.
Other (not so clever) alternatives are:
Retrieve all the records and after check the Guids
Do a single retrieve for each Guid
Because are only 40 records, I suggest to use late-bound to retrieve the records, in order to choose the minimal ColumnSet.
Useful links related to this issue:
Another question regarding Dynamics CRM LINQ limitations
Performance test Early Bound vs Late Bound