Cast Linq Result to Observablecollection

Cast LINQ result to ObservableCollection

Just use:

ObservableCollection<Foo> x = new ObservableCollection<Foo>(enumerable);

That will do the required copying. There's no way of observing changes to the live query - although the idea of an ObservableQuery<T> is an interesting (though challenging) one.

If you want an extension method to do this, it's simple:

public static ObservableCollection<T> ToObservableCollection<T>
(this IEnumerable<T> source)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
return new ObservableCollection<T>(source);
}

Casting a linq query to ObservableCollection

The type you select is SourceItem therefore you should use:

new ObservableCollection<SourceItem>(query.ToList());

LINQ query into ObservableCollection?

Basically, you need to pass IEnumerable<Staff_Time_TBL> result of the actual query to the database to initialize the ObservableCollection<Staff_Time_TBL> :

var linqResults = sql.Staff_Time_TBLs
.Where(staff => staff.Staff_No == SelectedEmployee.Key &&
staff.Date_Data == filterFrom &&
staff.Date_Data == filterTo);

var observable = new ObservableCollection<Staff_Time_TBL>(linqResults);

How do I return a element using LINQ query from an ObservableCollection?

I solved it using :

device = KnownDevices.Where(x => x.Name.Contains("NUR")).FirstOrDefault();

Linq Result into Observable collection throws parsing exception

Are you sure that your ObservableCollection is a collection of DataGridCollections? Is every element of your collection a DataGridCollection?

If not, but it is in fact a collection of MyType, change the word DataGridCollection below with MyType

Anyway, if you would check in your debugger the type of object d, you would notice that it is not an IEnumerable<DatagGridCollection>.

Just change your code to:

select new DataGridCollection()
{
p.sno,
...

If you want to detect the cause of this kind of errors in the future, my advice would be not to use the word var too much, and not to do too much statements at once.

IEnumerable<DataGridCollection> d = ...
Select new DataGridCollection
{
...
};

combine_audit_final_collection = new ObservableCollection<DatagGridCollection>(d);

It will be much easier to find your errors.

Make ObservableCollection class with LINQ

This worked for me:

//Since i dont know what Staff_Time_TBL has or represents, i will use this as example
public class SomeClass
{
public int number;
public string text;
public bool flag;
}

class Program
{
static void Main(string[] args)
{
//Your observable collection
ObservableCollection<SomeClass> observableSomeClass = new ObservableCollection<SomeClass>();

//I am assuming that in your LINQ query you are using a list or a structure, this is not important at all
List<SomeClass> listSomeClass = new List<SomeClass>();

//Im just adding some data to my list to work with
listSomeClass.Add(new SomeClass
{
number = 1,
text = "ONE",
flag = true
});
listSomeClass.Add(new SomeClass
{
number = 2,
text = "TWO",
flag = false
});
listSomeClass.Add(new SomeClass
{
number = 3,
text = "THREE",
flag = true
});

//This is a example LINQ query which is going to return me the ones that flag is equals to true (2)
var result = from node in listSomeClass
where node.flag == true
select new
{
node.number,
node.text,
node.flag
};

//THIS IS THE TRICK:
//Instead of trying making a convertion or a toList(), try iterating your result from your linq query and adding it to your observable structure
foreach (var r in result)
{
observableSomeClass.Add(new SomeClass
{
number = r.number,
text = r.text,
flag = r.flag
}
);

}

Console.ReadLine();
}

}

I hope this may help you.

convert linq results of decimal to ObservableCollectionstring

Try

ObservableCollection<string> cost = 
new ObservableCollection<string>((from i in context.Items
where i.Cost != null
&& i.Cost > 0
orderby i.Cost
select i.Cost).Distinct()
.AsEnumerable()
.Select(c => c.ToString()));

Since apparently the EF provider can't seem translate the ToString() call into SQL, putting in a call to AsEnumerable() will bring the query into memory and the ToString() call will use LINQ to Objects.

LINQ with ObservableCollection

Instead of creating a new collection, you could just remove the items you want from the original. This is preferable if, for example, you're working in WPF and have bound the observable collection to a control.

var itemsToRemove = ItemsObservableCollection.Where(
i => !selectedItemsObservableCollection.Any(y => y.name == i.Name)).ToList();

foreach (var item in itemsToRemove)
ItemsObservableCollection.Remove(item);

(Have to use ToList() to avoid a "collection was modified" error.)



Related Topics



Leave a reply



Submit