C# - Linq Expression To Return A List Of Strings Based On Field Count?

Nov 11, 2010

Say I have a table Comments with these columns: Id, Comment, Category, CreatedDate, CommenterIdI want to get the top 5 categories from the Comments table (based on the count of each category in that table). How can I do this in linq, to return either List or IQueryable?

View 2 Replies


Similar Messages:

ADO.NET :: Order By A Field In Dictionary Using Linq Expression?

Aug 16, 2010

I have an Entity class which has a Dictionary of fields called Data. Now I want to sort Entities by a field in Data. I was able to verify the Linq expression for using fields in a dictionary as e.Data["UserId"] as this seemed to work perfectly when I used LINQ to Objects. e.g. Entities.OrderBy(e => e.Data["UserId"])

However this same expression does not generate the correct NHibernate criteria and errors out finally with a NullReferenceException for propertyName at Hibernate.Loader.Criteria.CriteriaQueryTranslator.GetEntityName(ICriteria subcriteria, String propertyName)in CriteriaQueryTranslator.cs: line 541.

On debugging through NHibernate code, I figured that NHibernate.Linq.Visitors.MemberNameVisitor.GetMemberName(ICriteria rootCriteria, Expression expr) returns a null string in place of the field name after visiting expr using its visitor. This seems to be the cause of grief. I am using an older version of NHibernate (2.1). Are there any limitations in the NHibernate Linq visitor I should be aware of?

View 1 Replies

MVC :: Use Linq To Get A Field And Return?

Jun 18, 2010

I have a low level quiz. this is a mvc project

in PostRepository.cs, it has some code :

[Code]....

and then , in PostController.cs , I wanna a field in database call postStatus, the code is:
[Code]....

When I edit the post ,I hold the selectlist can show the option what is in database. But the code not work will.

then I use the Response.Write(postStatus); to test what happen. And the Browser show some code on top side :

[Code]....

I wanna what's up with the code and I want to know the suitable method.

View 2 Replies

C# - Sort A Gridview Of Linq Objects Based On A Derived Field?

Feb 16, 2010

I have written a page which uses Linq to query the database and bind the resulting IQueryable to a datagrid. I have a partial class which contains extra properties which derive their values based on other values brought in from the database.

Sorting works fine on fields that are actually in the database but not for the derived fields. When I attempt to sort on such a field I get an error saying "The member 'Trip.Difference' has no supported translation to SQL. how to allow sorting on these derived fields?

View 2 Replies

C# - Order A List Of Entities Based On A Field From A Separate Entity With A Relationship

Mar 6, 2011

I'm using Entity Framework for my object-relational mapping, and jqGrid for my grid.

I have an Employee entity, that contains a ContactID field. I have a Contact entity that contains fields FirstName and LastName.

I want to display a list of Employees in a grid, and give the user the ability to sort by FirstName and LastName.

Here's what I have right now:

public JsonResult GridData(string sidx, string sord, int page, int rows)
{
var pageIndex = Convert.ToInt32(page) - 1;
var pageSize = rows;
var totalRecords = GetAllEmployees().Count();
var totalPages = (int)Math.Ceiling(totalRecords / (float)pageSize);
IQueryable<Employee> employees = GetAllEmployees().
OrderBy(sidx + " " + sord).
Skip(pageIndex * pageSize).
Take(pageSize).ToArray();
}

As you can see, this only gives me the ability to sort by fields in the Employee entity, so I can't sort by FirstName and LastName.

How can I modify this to accomplish that?

View 1 Replies

To Increase Count By Comparing Two Strings Using Contains Function?

Aug 31, 2010

i am trying to increase count by comparing two strings using Contains function.
My code is,

int count = 0;
string strSize = "";
for (int i = 0; i < dts.Rows.Count; i++)[code].....

After executing this,totalCount should be equal to 4 but it is giving me 5,means first time when it is checking condition for strSize.Contains(strM[s]) it is getting true instead of false.Other thing when i am doing same in other application it is working fine.

code i wrote is,

int count=0;
string[] str = { "3", "4", "5", "10"};
string[] strM = {"1","2","3","4","5","10","45","50" };[code]...

tell me the difference between two.

View 4 Replies

Forms Data Controls :: Binding A Gridview Nested List Control Based On Field Value?

Dec 10, 2010

I need to bind a List control based on the Gridview Row's datakeyName value while the Gridview control is being loaded into the page. I tried the following but it does not work.The BulletedList control is nested inside a Gridview Control and they both bound to an ObjectDataSource control. In my code, I passed teh UserID into the UserID parameter and call the Select() method but still does not work. Basically, I want to list of a user's hobbies, therefore the hobbies control are driven by the userID. tell me how can I get it to work?

protected void UserGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
int UserID;

[code]...

View 1 Replies

Web Forms :: Write Scripts Which Used To Count Word In Strings?

Jun 23, 2010

I want to write scripts which used to count word in strings, This string include unicode characters.

View 7 Replies

Web Forms :: Regular Expression That Validates Empty Strings

Oct 16, 2010

I'm trying to write a regular expression that allows positive & negative floating point numbers, but doesn't allow empty strings and characters.I have managed to create this

[Code]....

but it allows empty strings and I can't figure out how to add [+-].

My program is a web app which asks the user to input a temperature.

View 2 Replies

SQL Server :: How To Return Strings From Stored Procedure

Jun 11, 2010

Long story short, it would be a lot easier for me to just return two values (from a many-column table, like combining columns) than to combine them in my asp project. I'm just having many problems with binding to a dropdownlist, and it would be easier not having to guess what index the column is I need :)

So, let's just say my table, "Building", has 5 columns - BuildingID, PracticeID, PracticeName, Address, & Zip (there are many more in my real table).

I already have a stored procedure that is "SELECT * FROM Building ORDER BY PracticeName". What I would like, is something that returns column values together with literal characters, like:

PracticeID - PracticeName (Address) AS First

I know I could use "SELECT PracticeName AS Name FROM Building" to simply change the name of the column I'd receive, but I want to add literal characters ( "-" and "(" and ")" and " ") with specific columns I choose.

Since I have about 20 columns in my table and I only want about 6, I don't need to SELECT *, but only those 6. That's easy enough. But since I would still like these six columns combined, I'm not sure what to do.

Again, what I want is to return something like:

PracticeID - PracticeName (Address) AS First BuildingID - Zip AS Second the "-" and "()" and " " need to be included and returned from SQL, not when I retrieve them)

View 4 Replies

AJAX :: AutoComplete Extender Return Concatenated Strings?

Mar 3, 2011

The problem is I have an autocomplete extender on my page for a search purpose. I have a webservice pulling data from my DB, a little Linq query and I am pulling data just how i want it to be displayed. But then a problem came, I would like one thing to happen to resolve this following issue. First, observe codes from my web service (it works!)

[Code]....

[Code]....

observe After user select an item (I am getting that) 1) How do I ONLY have the string value of ' LoalArea ' in my textboxafter the user makes a selection disregarding the other ( " , " and " County " ) added string. I still would like my users to see them all ([LocalArea] + ", " [County]) in the list dropdown but not in the textbox after they selected .

View 3 Replies

Regular Expression / Input String Is Valid Based On The Given Regular Expression?

Sep 20, 2010

i have to check if an input string is valid based on the given regular expression.. this is the regular expression.

(a(bb+aaa+((ab)*bb+(aba)*aa)(a+b)*) + (b(aa+bbb+((ba)*aa+(bab)*bb)(a+b)*)

+ is an OR

* is a LOOP, it is optional, it may be done as many as a user wants.

i've tried many ways to solve the problem that my professor gave me but my knowledge isn't that enough to solve this problem..

View 3 Replies

Encrypt Connection Strings When Using Linq To SQL?

May 31, 2010

What is the best practice for encrypting the connectionStrings section in the web.config file when using LINQ TO SQL?

View 2 Replies

ADO.NET :: LINQ And Web Application Connection Strings

Dec 25, 2010

i want define connectionSreing in the web.config and then use it for DataContext ConnectionString. i do that : 1. i creat a class in App_Code :(MisaghDB is my database)

partial class MisaghDataContext
{
partial void OnCreated()
{
this.Connection.ConnectionString =
ConfigurationManager.ConnectionStrings["MisaghDBConnectionString"].ConnectionString;
}
// or
public partial class MisaghDataContext
{
public MisaghDataContext() : base (ConfigurationManager.ConnectionStrings["MisaghDBConnectionString"].ConnectionString)
{
OnCreated();
}
}
}

2. Add the connection string to my web.config file:

<configuration>
<connectionStrings>
<add name="MisaghDBConnectionString" connectionString="Data Source=NAZLIN-HP;Initial Catalog=MisaghDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>

3. Right click on the DBML file design surface and chose properties. Select "none" for the Connection property.now should i change DBML file designer.cs (Misagh.designer.cs) ? The code should I delete in this file(DBMLfile designer.cs)? this is a part of my DBMLfile designer.cs :

[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="MisaghDB")]
public partial class MisaghDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
public NorthwindDataContext() :
base(global::WindowsFormsApplication2.Properties.Settings.Default.NorthwindConnectionString, mappingSource)
{
OnCreated();
}
public NorthwindDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public NorthwindDataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public NorthwindDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public NorthwindDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
............}

View 4 Replies

Installation :: Uniting All The Connection Strings In Linq To Sql?

Jun 14, 2010

I have a datalayer that uses Linq to SQL , inorder to maintain properly I removed various references to the connection strings to be placed in the same location.

So used the base(ConfigurationSettings.ConnectionString[""].ConnectionString,mappingsource) in the dbml.designer.cs file.

But doing this we have to define the connection strings in the web.config of all the projects that are going to use the data layer.. instead is there a way where in I can define the connection strings in the app.config of the data layer project and have the dbml.designer.cs file to pick from this one place?

View 2 Replies

Return Count Of True / False In A Column Of A Sql Table In C#?

Jan 18, 2011

How to return count of true/ false in a column of a table. I have a table in sql containing 5 columns with bollean type. Now I have to return the number of trues and falses in each column and display on my frontend.

View 1 Replies

Forms Data Controls :: Get Count Sms In Day And Return SmsDate?

Mar 3, 2011

data table

i want to get count sms in day and return smsDate and Sms Cont

now i want use these value in chart

View 1 Replies

Forms Data Controls :: Disable/Invisible Field In Gridview Based On Another Field?

Jul 9, 2010

I have 2 fields in my gridview one called ScriptType the other BagNo, when a user click the edit button, I would like the BagNo filed to be disabled if the ScriptType field is = "TTA" and enabled otherwise. How can I do this?..something along these lines see below..I am using VB..

[code]...

View 14 Replies

ADO.NET :: How To Use Count In LINQ

Dec 1, 2010

Im new for LINQ, how can i count the records in this code

[code]....

So i can say Count records and its = PValue so i can use that in a EVAL(PValue) and get the total value of the records.

View 1 Replies

C# - GridView Sort Expression Must Return Not Empty Data

Mar 1, 2010

My program running good. But i try to add up-down icon on my gridView header. but if i make it, My break point should drop below "if comparison" For example; field.SortExpression=Category but every time CustomersGridView.SortExpression is empty when gridview.SortExp is not empty.

foreach (DataControlField field in CustomersGridView.Columns)
{
if (field.SortExpression == CustomersGridView.SortExpression)
{
return CustomersGridView.Columns.IndexOf(field);
}
}
i need :
foreach (DataControlField field in CustomersGridView.Columns)
{
if (field.SortExpression == "Category")
{
return 2;
}
}
CustomersGridView.SortExpression must not be empty!!!!!
MY source:
<head runat="server">
<title></title>
<link type="text/css" href="StyleSheet.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView
ID="gvCustomers" runat="server" CssClass="tablestyle"
AllowSorting="true"
OnRowDataBound="GvCustomers_RowDataBound" AutoGenerateColumns="false">
<AlternatingRowStyle CssClass="altrowstyle" />
<HeaderStyle CssClass="headerstyle" />
<RowStyle CssClass="rowstyle" />
<Columns>
<asp:BoundField HeaderText="Kategori" DataField="Category" SortExpression="Category" />
<asp:BoundField HeaderText="Tarih" DataField="Date" SortExpression="Date" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
protected void GvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView gridView = (GridView)sender;
if (gridView.SortExpression.Length > 0)
{
int cellIndex = -1;
foreach (DataControlField field in gridView.Columns)
{
if (field.SortExpression == gvCustomers.SortExpression)
{
cellIndex = gridView.Columns.IndexOf(field);
break;
}
}
if (cellIndex > -1)
{
if (e.Row.RowType == DataControlRowType.Header)
{
// this is a header row,
// set the sort style
e.Row.Cells[cellIndex].CssClass +=
(gridView.SortDirection == SortDirection.Ascending
? " sortascheader" : " sortdescheader");
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
// this is an alternating row
e.Row.Cells[cellIndex].CssClass +=
(e.Row.RowIndex % 2 == 0
? " sortaltrow" : " sortrow");
}
}
}
}

View 1 Replies

ADO.NET :: Using Linq To Get The Count Of Records?

Nov 24, 2010

I need to find the count of records between the first and last day of the current month.I am very new to linq and having doubts in getting the count, below is the code i have written.

[code]....

View 1 Replies

Basic SQL Count With LINQ?

Mar 24, 2011

I have a trivial issue that I can't resolve. Currently our app uses Linq to retrieve data and get a basic integer value of the row count. I can't form a query that gives back a count without a 'select i'. I don't need the select, just the count(*) response. How do I do this? Below is a sample:

return (from io in db._Owners
where io.Id == Id && io.userId == userId
join i in db._Instances on io.Id equals i.Id **select i**).Count()
;

View 3 Replies

C# - Determine If A String Contains Any Matches Of A List Of Strings?

Mar 23, 2011

I have a list of strings:

var listOfStrings = new List<string>{"Cars", "Trucks", "Boats"};

and I have a vehicles options which has a Name field.I want to find the vehicles where the name matches one of the items in the listOfStrings.I'm trying to do this with linq but can't seem to finish it at the moment.

var matchingVehicles = Vehicles.Where(v => v.Name == one of the listOfStringItem)

View 6 Replies

Getting Field Count From Database

Feb 8, 2011

I am having a problem that I want to get a list of items(like categories) and count of records corresponding to those items (like subcategories).. I have two tables (categories, and users). I want to get the list of categories from categories table and the count of subcategories from the users table...I donot know how to do this. e.g. "select categories from categories" and "select count(subcategories) from users where category = ?"

View 5 Replies

How To Get C# Return Dropdown List To Return Value As Int

Jun 8, 2010

How to get asp.net return drop down list to return value as int

I want to pass the value to a stored procedure as an integer. But the default appears to be as a string which is not what the store procedure is expecting.

Is there a good way to return the list values as ints?

I suspect I can you set the value on the change selection event, is there another way?

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved