linq-to-entities

Linq to objects when object is null VS Linq to SQL

Deadly 提交于 2020-01-21 11:26:30
问题 I have this Linq to object query: var result = Users.Where(u => u.Address.Country.Code == 12) I get an exception if the Address or the Country are null. Why this query doesn't check if the address is null and just after that procced? This way I won't need to write this terrible query: var result = Users.Where(u => u.Address != null && u.Address.Country != null && u.Address.Country.Code == 12) In Linq to SQL the first query wiil do the job(from other reasons of course). Is the a way to avoid

Linq to objects when object is null VS Linq to SQL

做~自己de王妃 提交于 2020-01-21 11:24:48
问题 I have this Linq to object query: var result = Users.Where(u => u.Address.Country.Code == 12) I get an exception if the Address or the Country are null. Why this query doesn't check if the address is null and just after that procced? This way I won't need to write this terrible query: var result = Users.Where(u => u.Address != null && u.Address.Country != null && u.Address.Country.Code == 12) In Linq to SQL the first query wiil do the job(from other reasons of course). Is the a way to avoid

How to determine if Navigation Property in the Entity Framework is set without loading the related records

爱⌒轻易说出口 提交于 2020-01-21 04:23:26
问题 I'm not sure about Navigational Properties in EF 4 so I would kindly ask you an explanation. Lets imagine this scenarios: A) I have two Entities A and B with a relation N to N (many to many) and tree Table in my Data Base A and B and a Link Table AB with two Foreign Key . In this scenario EF create a Navigational Property lets call it X and also a XReference . B) I have two Entities A and B with a relation 1 to N (one to many) and two Table in my Data Base A and B with one Foreign Key . In

How to make a Lambda expression Reusable in multiple LINQ Queries

早过忘川 提交于 2020-01-16 20:37:13
问题 I have the following LINQ query: var result = from person in dbContext.Person select new { FirstName = person.FirstName, LastName = person.LastName, // I want to save this logic JobCount = person.Jobs.Count(x => x.Completed) }; } To avoid repeating myself in other LINQ queries, I would like to make the JobCount lambda logic available for use in other queries. I thought I might be able to use Func<Person, int> , like so: public Func<Person, int> GetCompletedJobsForPerson = person => person

Error when splitting and replacing chars C# email with LINQ Array Index not supported in LINQ

心已入冬 提交于 2020-01-16 20:06:08
问题 This throws an error 'ArrayIndex' is not supported in LINQ to Entities. so what would be an alternative for this as the split and replace fails? from S in db.Students where S.ID = ID select new { S.EMAIL.Split('@')[0].Replace(".", " ,"), S.NAME }; This is for a list of Students and this is how I currently can add them. mylist.DataValueField = "email"; mylist.DataTextField = "name"; This fails: S.EMAIL.Split('@')[0].Replace(".", " ,") Please help. Thanks 回答1: If you want it to run on the SQL

Error when splitting and replacing chars C# email with LINQ Array Index not supported in LINQ

大兔子大兔子 提交于 2020-01-16 20:05:03
问题 This throws an error 'ArrayIndex' is not supported in LINQ to Entities. so what would be an alternative for this as the split and replace fails? from S in db.Students where S.ID = ID select new { S.EMAIL.Split('@')[0].Replace(".", " ,"), S.NAME }; This is for a list of Students and this is how I currently can add them. mylist.DataValueField = "email"; mylist.DataTextField = "name"; This fails: S.EMAIL.Split('@')[0].Replace(".", " ,") Please help. Thanks 回答1: If you want it to run on the SQL

On data binding: LINQ to Entities does not recognize the method 'System.String ToString()' method

有些话、适合烂在心里 提交于 2020-01-16 04:02:24
问题 I'm using LINQ to Entities My GridView is the following : <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" EmptyDataText="No Data" CellPadding="3" CellSpacing="1" AllowSorting="True" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" CssClass="gridview" OnSorting="GridView1_Sorting" HorizontalAlign="Center"> <AlternatingRowStyle

Having troubles loading related entities (Eager Load) with ObjectContext.CreateQuery (Entity Framework and Repositories)

ⅰ亾dé卋堺 提交于 2020-01-16 02:06:27
问题 Here's a bunch of things I tried... hopefully you can extrapolate from it what I'm trying to do and what I'm doing wrong. Okay so I'm having problems with loading related entities when using this DoQuery: public ObjectQuery<E> DoQuery(ISpecification<E> where) { return (ObjectQuery<E>)_ctx.CreateQuery<E>("[" + typeof(E).Name + "]").Where(where.EvalPredicate); } If I just use this, I end up getting an object back that contains all proper parameters except the ones that are related entities...

Sort child objects while selecting the parent using LINQ-to-Entities

女生的网名这么多〃 提交于 2020-01-15 01:23:10
问题 Imagine you've got some Entity Framework entities that look like this (obviously not these specific classes, but the autogenerated ones with all the Entity Framework plumbing; these are just for illustration): public class Parent { public int ID { get; set; } public List<Child> Children { get; set; } } public class Child { public int ID { get; set; } public Parent Parent { get; set; } public int Number { get; set; } } I have a LINQ query that looks like this: from parent in context.Parents

Comparing performance of generated queries for Any() vs Count() in Entity Framework 5

怎甘沉沦 提交于 2020-01-14 14:24:31
问题 In my project I use Entity Framework 4.4.0.0 and I have the following dilemma. I have to check if an user is activated. My query looks like: Any() _context.Users.Any(u => u.Id == userId && u.IsActivated); The generated sql is: SELECT CASE WHEN ( EXISTS (SELECT 1 AS [C1] FROM [dbo].[Users] AS [Extent1] WHERE ( [Extent1].[Id] = @p__linq__0 ) AND ( [Extent1].[IsActivated] = 1 )) ) THEN cast(1 AS BIT) WHEN ( NOT EXISTS (SELECT 1 AS [C1] FROM [dbo].[Users] AS [Extent2] WHERE ( [Extent2].[Id] = @p_