Hi
Is Entity Framework worth moving to for a new small app? About 10 tables and a WinForms app.
ie the alternative being DataTables/DataRows or Linq-to-SQL
I'm having problems when using linq on a datatable.asenumerable().
This throws InvalidCastException.
DateTime date=r.Field<DateTime>("date");
This works fine.
DateTime date = DateTime.Parse(r.Field<string>("date"));
What am I missing?
Regards Sven
if i have this code today to find out a sum total using LINQ:
return (MyArray.Sum(r => r.Trips);
and i want to only include itms where r.CanDrive == true.
can you add a condition into a single linke lambda expression? how would you do this
Hello.
I have a short question. Im my current project I'm using LINQ-to-SQl. That is the best way to determine if table has record with specific ID?
Thanks in advance.
Consider I have a DataContext db, and there is an Entity class User. So when System.Data.Linq.Table<User> table = db.GetTable<User>(); is called for the first time, does it pull the data from the database immediately, does it use deferred execution, or were the data already loaded from database when db was initialized?
Using InsertOnSubmit seems to have some memory overhead.
I have a System.Data.Linq.Table<User> table. When I do table.InsertOnSubmit(user) and then int count = table.Count(), the memory usage of my application increases by roughly the size of the User table, but the count is the number of items before user was inserted. So I'm guess an enumeration after InsertOnSubmit will create a copy of the table. Is that true?
Does any one know of an example on how to store an image in a SQL Server CE database?
What data type should the column be? (I am guessing binary.)
I use Linq-To-Datasets. Is it possible using that to put the image into the database and pull it out again later?
Thanks for any advice.
These web articles uses separate Save() and Update() methods in the repository pattern.
I am using repository pattern.
How can I write a SaveOrUpdate() method in Entity Framework with the help of ObjectContext and in Linq-To-SQL with the help of DataContext?
That is, how can I write a single method that shall do both save and update job?
I have ~10,000 records would like to keep in a collection in memory and execute LINQ queries against it. This collection should be available for all the users in the application domain and can access concurrently. I’m looking for a .NET collection that supports multithreading can query asynchronously and efficiently without any threading issues. Any suggestions on deciding a collection for this?
If I try:
Groups = students.Remove(f => f.StudentID.Equals(studentID));
I get an error on this line: f => f.StudentID.Equals(studentID)
I am having difficulty from my previous posts here linq deleted users still associated with groups and here Delete method in WCF
So I thought maybe I could delete the students contained within groups but I get an error Cannot convert lamda expression to type Student because it is not a delegate type.
This lambda does not compile, but I do not understand why.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using LinqKit;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var barModel = new BarModel();
string id = "some";
Console.WriteLine(barModel.subFor(id).ToString());
// output: m => (True AndAlso (m.key == value(ConsoleApplication2.Bar`1+<>c__DisplayClass0[ConsoleApplication2.Model]).id))
Console.ReadKey();
var subworkitems = barModel.list.Where(barModel.subFor(id).Compile());
// Exception {"variable 'm' of type 'ConsoleApplication2.Model' referenced from scope '', but it is not defined"}
Console.WriteLine(subworkitems.ToString());
Console.ReadKey();
}
}
class Bar<TModel>
{
public Bar(Expression<Func<TModel, string>> foreignKeyExpression)
{
_foreignKeyExpression = foreignKeyExpression;
}
private Expression<Func<TModel, string>> _foreignKeyExpression { get; set; }
public Expression<Func<TModel, bool>> subFor(string id)
{
var ex = forTargetId(id);
return ex;
}
public Expression<Func<TModel, bool>> forTargetId(String id)
{
var fc = _foreignKeyExpression;
Expression<Func<TModel, bool>> predicate = m => true;
var result = predicate.And(m => fc.Invoke(m) == id).Expand();
return result;
}
}
class Model
{
public string key;
public string value;
}
class BarModel : Bar<Model>
{
public List<Model> list;
public BarModel() : base(m => m.key)
{
list = new List<Model>();
}
}
}
I've got a ILookup< string, List<CustomObject> > from some linq I've done.
I'd like to now iterate over the results:
foreach(IGrouping<string, List<CustomObject>> groupItem in lookupTable)
{
groupItem.Key; //You can access the key, but not the list of CustomObject
}
I know I must be misrepresenting a IGrouping as a KeyValuePair, but now I'm not sure how to access it properly.
I'm trying to cast an IEnumerable of an inherited type to IEnumerable of base class.
Have tried following:
var test = resultFromDb.Cast<BookedResource>();
return test.ToList();
But getting error:
You cannot convert these types. Linq to Entities only supports conversion primitive EDM-types.
The classes involved look like this:
public partial class HistoryBookedResource : BookedResource
{
}
public partial class HistoryBookedResource
{
public int ResourceId { get; set; }
public string DateFrom { get; set; }
public string TimeFrom { get; set; }
public string TimeTo { get; set; }
}
public partial class BookedResource
{
public int ResourceId { get; set; }
public string DateFrom { get; set; }
public string TimeFrom { get; set; }
public string TimeTo { get; set; }
}
[MetadataType(typeof(BookedResourceMetaData))]
public partial class BookedResource
{
}
public class BookedResourceMetaData
{
[Required(ErrorMessage = "Resource id is Required")]
[Range(0, int.MaxValue, ErrorMessage = "Resource id is must be an number")]
public object ResourceId { get; set; }
[Required(ErrorMessage = "Date is Required")]
public object DateFrom { get; set; }
[Required(ErrorMessage = "Time From is Required")]
public object TimeFrom { get; set; }
[Required(ErrorMessage = "Time to is Required")]
public object TimeTo { get; set; }
}
The problem I'm trying to solve is to get records from table HistoryBookedResource and have the result in an IEnumerable<BookedResource> using Entity Framework and LINQ.
UPDATE:
When using the following the cast seams to work but when trying to loop with a foreach the data is lost.
resultFromDb.ToList() as IEnumerable<BookedResource>;
UPDATE 2:
Im using entity frameworks generated model, model (edmx) is created from database, edmx include classes that reprecent the database tables.
In database i have a history table for old BookedResource and it can happen that the user want to look at these and to get the old data from the database entity framework uses classes with the same name as the tables to receive data from db. So i receive the data from table HistoryBookedResource in HistoryBookedResource class.
Because entity framework generate the partial classes with the properties i dont know if i can make them virtual and override.
Any suggestions will be greatly appreciated.
Hi,
Suppose 2 classes, Person and Pet. Each person has a collection of 1 or more pets.
How do i group the Person in to a collection where they share the same pets.
Example:
Person 1: Cat, Dog, Spider
Person 2: Cat, Spider, Snake
Person 3: Dog
Person 4: Spider, Cat, Dog
Person 5: Dog
What i want as a result is this:
Group 1: Person 1, Person 4
Group 2: Person 3, Person 5
Group 3: Person 2
How do i achieve this using LINQ?
I created a Windows Forms Application to which I added a DataGridView and LINQ to SQL Classes from one of my databases. I can successfully bind one of my database's tables to my DataGridView:
var dataSource = from c in _db.NetworkedEquipments
select c;
dataGridView1.DataSource = dataSource;
However, the foreign keys get duplicated, that is, the columns appear twice. How can I prevent this?
I'm using LinqToSql like this with a CheckBoxList in ASP.NET:
var teachers = from x in dc.teachers select x;
cbl.DataSource = teachers;
cbl.DataTextField = "name";
cbl.DataValueField = "teacherID";
cbl.DataBind();
I want to display both "firstname" and "name" in the DataTextField however.
I found this solution but I'm using LINQ:
http://stackoverflow.com/questions/839223/concatenate-two-fields-in-a-dropdown
How do I do this?
I have two long type columns that I want to concat during sql query. Using Linq to Entities making it impossible, because it only supports String.Concat(string, string).
I'd like to know how can I implement this function myself and add it to the L2E framework.
Hello!
I have IEnumerable - that i recieved(converted) from dattabase.
i need to select from this collection only unique strings.
i suppose that i need to use linq group by string...
please help.
I have a requirement to have a multiline textbox that accepts any text from any language and stores this into the database for later use.
I am using Linq with ASP .NET 3.5 with a SQL Server 2005 database.
Any/all information you can provide me with is much appreciated :D
Using LINQ 2SQL , How can i call a stored procedure which returns either 1 or 0 when being executed.My procedure has 3 input params.I want to know the return value from procedure in my code too.
Suppose I have a class
public class Foo
{
public Bar { get; set; }
}
Then I have another class
public class Gloop
{
public List<Foo> Foos { get; set; }
}
What's the easiest way to get a List of Foo.Bars?
I'm using C# 4.0 and can use Linq if that is the best choice.
My first thought was something like
Hi,
I have an array of type string that looks like this:
"test1|True,test2|False,test3|False,test4|True".
This is essentially a 2d array like so
[test1][True]
[test2][False]
[test3][False]
[test4][True].
I want to convert this into a dictionary<string,bool> using linq, something like:
Dictionary<string, bool> myResults = results.Split(",".ToCharArray).ToDictionary()
any ideas?
I have an List<int> which contains 1,2,4,7,9 for example.
I have a range from 0 to 10.
Is there a way to determine what numbers are missing in that sequence?
I thought LINQ might provide an option but I can't see one
In the real world my List could contain 100,000 items so performance is key
How can we get an autogenerated Id from the Table While Inserting?
I have three tables
Project
ProjectXMilestone
Milestone
and on Insert of Milestone I also want to insert a row in 2nd Table so I need MilestoneId?so how can I get that?
I am using Linq.
Is there any way showing the detailed exception for String or binary data would be truncated.
I use LINQ to SQL and i cant figure out which column is that!?
Tried numerous things but i get the same meaningless error i got from within Visual Studio.
Also I use .NET 3.5 SP1, but errors are still returned useless.