Linq: additional parameter

I have the following line: orderBaseData.Single(o => o.Id == order.Id).OrderMessages.Count; I want to filter this a bit more. Each OrderMessage has a variable called hideFromUser, and I want to get the count of the OrderMessages where this is set to

How to convert this SQL statement to linq in C #?

Please help me to convert this SQL statement to Linq in C#: string cmd = "SELECT * FROM dbo.Personnel WHERE (dbo.Personnel.FName LIKE @FName + '%' AND dbo.Personnel.LName LIKE @LName +'%')"; if (objPPersonnel.PersonnelCode != 0) { cmd += "A

Entity to model the mapping in LINQ

I have article and author classes. Fetch articles like so and map entity to model: public List<Model.Article> GetArticleList() { using (var db = new ArticlesContext()) { return db.Articles.Select(t => new Model.Article() { Author = MapUserEntityT

EF6: Use reference / search data with IQueryable

I want to use a pre-loaded lookup data from a list within a query. I need the query to return as IQueryable because it is used in a grid & being paged (not included here). I need the Labels to load from the lookup to avoid joins (more are in the actu

How to write dynamic LINQ queries using DBContext in C #

I am modifying my project to use DBContext instead of ObjectContext. My existing code var query = context.Vehicles.OrderBy("it.VehicleType.VehicleTypeID"). GroupBy("it.VehicleType.VehicleTypeID", "Min(it.ODO_Reading) AS MinRunVehi

Linq FirstOrDefault - a coating

i have the following code to determine a age from a Person var pList = ctx.Person.Where(x => x.Create > Date); int Age = pList.Where(x => x.ID == "foo").FirstOrDefault().Age ?? 20; I pick a Person by ID, if it doesn't exist the default

Xelement Linq with a null value?

I try getting all the Xelement value and insert into an array, however when the Xelement is null, it will show the error as below when insert into array: Object reference not set to an instance of an object. Is it possible to remove the null value? O

how to reorder the xml

Workign on xml reorder My XML is as follows: <Subjects> <Subject> <Name></Name> <Height></Height> <Addresss> <Address> <City>AB</City> </Address> </Addresses> </Subject> <Sub

Filter a list using except on multiple properties

I am trying to write a method which will ultimately give me a collection of records which no longer have a corresponding record within a different collection to which I am referencing. The structure is something similar to: public class A { int id {g

Read XML to Datatable dynamically

I have the following xml string that I am attempting to query into a datatable based on a dynamic set of columns. I do not have any control over the xml, it is being passed in from an external source. XML: <VmapMessage> <MessageBody> <Claim

LINQ Query Throwing a Cast Exception

I am attempting a LINQ query in a VB.NET WPF solution that bridges two datatables and groups by a Zone. I am receiving an "Invalid Cast Exception" on these rows: .MfstCt = ZoneGroup.Count(Function(c) c.Field(Of String)("ConsignmentNumber&qu

Group and sum with select a lot of C #?

I have an incoming array with a count per row and a string which represents one or more key's split by underscore. for each key I'd like to group and sum the total where if the key appears in a row with a total of 5, each item split by the underscore

Understand a LINQ query - ascending orderby

I don't understand why code A is correct and code B is incorrect: code A IEnumerable<decimal> loanQuery = from amount in loanAmounts where amount % 2 == 0 orderby amount select ascending amount //this line code B (incorrect) IEnumerable<decimal&g

ExpandoObject with a selection clause

I have to populate below class in Linq Query public class Emp { public string name {get; set;} public dynamic Obj { get; set; } public Emp() { Obj = new ExpandoObject(); } } public LoadData() { var emp = (from d in dbContext.Employees select new Emp

Convert ?: Instruction to handle more than 2 responses

I'm fairly new to programming but have been tasked with maintaining some applications that were created by a previous employee. I have a ?: statement that now needs to handle more than a true or false statement but I'm not sure how to go about doing

Dynamically create GridView

I want to create a GridView that displays records for PDF Files. These records can have meta data attached that is customizeable by the user, so they can create their own columns and enter their own information in there. I then want it to be displaye

Edit the foreach loop in the LINQ query

I stuck in an easy scenario. I have a List<string> object, all of its items has the body of: item_1_2_generatedGUID //generatedGUID is Guid.NewGuid() but there may be much more numbers item_1_2_3_4_5_generatedGUID etc now, I'm wondering how to chang

LINQ to Entities how to update a record

Okay, so I'm new to both EF and LINQ. I have figured out how to INSERT and DELETE but for some reason UPDATE seems to escape my grasp. Here is a sample of my code: EntityDB dataBase = new EntityDB(); Customer c = new Customer { Name = "Test", Ge

how to break the loop if the condition satisfies in linq?

I have list of ints consisting of {1,2,3,1,2,1,5,1,2,1,1} I like retrieve all 1 located before 5 . I need skip all other items after 5 .The End list will be {1,1,1} How can i do this using linq?Try this: using System.Linq; class Program { static void

Children objects in MongoDB

I have been following along with Rob Conery's Linq for MongoDB and have come across a question. In the example he shows how you can easily nest a child object. For my current experiment I have the following structure. class Content { ... Profile Prof